Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
661 views
in Technique[技术] by (71.8m points)

pascal - 函数处于循环中时不会创建随机序列-Pascal(Function doesn't create a random sequence when it's in cycle - Pascal)

My code is written on PascalABC.NET.

(我的代码写在PascalABC.NET上。)

Function (RandomRoute) is working, but when I try to run it several times in cycle it doesn't work in a proper way.

(函数(RandomRoute)正在运行,但是当我尝试以周期方式运行几次时,它无法以适当的方式运行。)

Every iteration it has to generate a new sequence.

(每次迭代都必须生成一个新序列。)

输出 .

(。)

On this picture you can see what I mean.

(在这张照片上,您可以看到我的意思。)

I don't understand where the problem is.

(我不明白问题出在哪里。)

PS sorry for my english

(PS对不起我的英语)

function RandomRoute (n : integer; a : TMatrix) : List<integer>;
var
  k,i,j,iRand, pathLength: integer;
  flag : boolean;
  tempList : List<integer> := new List<integer>;
Begin
  randomize; 
  for k := 0 to (n-1) do
  begin
    repeat
      iRand := random(0, (n-1));
      if (tempList.Contains(iRand)) then       
      begin
        flag := false;
      end
      else
      begin
        flag := true;
        tempList.Add(iRand);
      end;            
    until (flag);
  end;   

  tempList.Add(tempList[0]);    
  pathLength := 0;
  for var z : integer := 0 to (n-1) do
  begin
    i := tempList[z]; j := tempList[z+1];
    pathLength := pathLength + a[i,j];
  end;
  tempList.Add(pathLength);
  Result := tempList;
End;

MAIN FUNCTION

(主功能)

BEGIN
  var randRoute : List<integer> := new List<integer>;
  var minRoute : List<integer> := new List<integer>;

  routeLength := 0; 

  writeln ('City Count:');
  readln (n); 
  readln (firstcity);
  MatFullRandom(n, mass); 
  massCopy := mass;

  DispMat (n, mass);
  GreedyAlg(firstcity, n, mass);

  Writeln ('RANDOM ROUTES:');

  randRoute := RandomRoute(n, massCopy);
  minRoute := randRoute;

  for var z : integer := 0 to 10 do
  begin      
    randRoute := RandomRoute(n, massCopy);
    if (randRoute.Last < minRoute.Last) then minRoute := randRoute;
    writeln('Random Route ', randRoute);    
  end;

  writeln('The best from randomRoute: ',minRoute);

  MatrixRandomRoute(n, minRoute, massCopy, massRed);
  DispMat(n, MassRed);
  DrawGraph(n, mass); 
END.
  ask by Lika Barken translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The Randomize() function initializes the random number generator, with a number, typically derived from the system clock.

(Randomize()函数使用通常从系统时钟派生的数字初始化随机数生成器。)

Successive calls to Randomize() in a loop together with a call to Random() can yield the same result repeatedly.

(循环中对Randomize()连续调用以及对Random()的调用可以重复产生相同的结果。)

Therefore, call Randomize() only once in the program.

(因此,在程序中仅调用一次Randomize() 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...