Skybuck Flying
2015-05-14 11:46:59 UTC
Hello,
I am a bit mystified about the "Random" routine in Delphi.
When I click "find declaration" in Delphi XE7... it will navigate to a
function which will look like:
function Random(const ARange: Integer): Integer;
So at first glance this function seems to return values between 0 and MaxInt
or so...
However Random is somehow capable of initializing doubles with values
between 0 and 1 ?
How can this be ??? Please clearify/solve this mystery for me ?!?
Example code:
program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
var
vIndex : integer;
vDouble : double;
begin
try
{ TODO -oUser -cConsole Main : Insert code here }
for vIndex := 0 to 1000 do
begin
vDouble := Random;
writeln( vDouble:16:16 );
end;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
ReadLn;
end.
Sample of output:
0.7212548679672182
0.2104894851800054
0.4930866835638881
0.6567967527080327
0.3219850626774132
0.5962043243926018
0.5341286454349756
0.4350875786039978
0.1325552123598754
0.5131898613180965
0.7825037101283669
0.7080669899005443
0.2223086454905570
0.4329185977112502
0.9693536832928658
0.7503404992166907
0.8087553470395505
0.4153525677975267
0.0065492829307914
0.9315644309390336
0.5416906108148396
0.4670365999918431
0.4646564479917288
0.5437774641904980
Bye,
Skybuck.
I am a bit mystified about the "Random" routine in Delphi.
When I click "find declaration" in Delphi XE7... it will navigate to a
function which will look like:
function Random(const ARange: Integer): Integer;
So at first glance this function seems to return values between 0 and MaxInt
or so...
However Random is somehow capable of initializing doubles with values
between 0 and 1 ?
How can this be ??? Please clearify/solve this mystery for me ?!?
Example code:
program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
var
vIndex : integer;
vDouble : double;
begin
try
{ TODO -oUser -cConsole Main : Insert code here }
for vIndex := 0 to 1000 do
begin
vDouble := Random;
writeln( vDouble:16:16 );
end;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
ReadLn;
end.
Sample of output:
0.7212548679672182
0.2104894851800054
0.4930866835638881
0.6567967527080327
0.3219850626774132
0.5962043243926018
0.5341286454349756
0.4350875786039978
0.1325552123598754
0.5131898613180965
0.7825037101283669
0.7080669899005443
0.2223086454905570
0.4329185977112502
0.9693536832928658
0.7503404992166907
0.8087553470395505
0.4153525677975267
0.0065492829307914
0.9315644309390336
0.5416906108148396
0.4670365999918431
0.4646564479917288
0.5437774641904980
Bye,
Skybuck.