Math.random() to Delphi

alpires
alpires used Ask the Experts™
on
Hi Experts,

How to convert this function "Math.random()" (javascript) to delphi ?

Thanks for some help
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2011

Commented:
There is a Random function in Delphi under the System Unit:
procedure TForm1.Button1Click(Sender: TObject);
begin
  Randomize;
  showmessage(inttostr(Random(10)));
end;

Open in new window


When you call Random(x) then the possible result will be between 0 and x-1.
Commented:
In Javascript math.random() returns an extended value between 0 and 1.
to get a random between a range starting with 0 and maxnumber
maxnumber is multiplied to the function and converted to integer using Math.floor.

In delphi Random is same as Math.Random() - it returns an extended value between 0 and 1 and to get a values within  a range 0 to maxnumber,same thing is done
multiply with maxnumber and converted to integer using floor function.

Function GetARandomNumber(MaxNumber:Integer)
var anynumber:Integer;
begin
Randomize;
anynumber:= floor(random * MaxNumber);
result := anynumber;
end;

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial