0

How to convert this part of Matlab code to Delphi?

for i=1:popsize 
    fi=rand(1,dimension); % Generate a vector of uniform random numbers
    p=pbest(i,:);
    pbest(i,:)=x(i,:);
end

My code:

for i:= 1 to popsize do
begin
  fi:= // which function generates vector of uniform random numbers in Delphi?
  for k :=1 to popsize do
  begin
    p:=pbest(i,k);
    pbest(i,k):=x(i,k);
  end;
end;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
shdotcom
  • 157
  • 1
  • 11

1 Answers1

1

You can call Random function to generate a uniformly distributed random value. Calling Randomize once makes Random generate different values in each run.

var
  fi: array of Double;
  J: Integer;
begin
  Randomize;
  for J := 0 to dimension - 1 do
    fi[J] := Random;
end;
saastn
  • 5,717
  • 8
  • 47
  • 78
  • Don't call Randomize too often. Very easy to destroy randomness. Call it once at start up. Also, usually better to get a real PRNG. – David Heffernan Feb 08 '16 at 01:13