Random function
Generate random number.
Declaration
function Random(l: LongInt): LongIntDescription
Random returns a random number larger or equal to
0 and
strictly less than
L. For negative values of
L the behaviour is undefined.
If the argument
L is omitted, a Real number between 0 and 1 is returned
(0 included, 1 excluded).
The Free Pascal implementation of the Random routine uses a
Mersenne Twister algorithm to simulate randomness. This implementation has a better
statistical distribution than for example a Linear Congruential generator
algorithm, but is considerably slower than the latter. If speed is an issue,
then alternate random number generators should be considered.
Note that the fact that a Mersenne Twister is used is an implementation detail,
which can be changed at any point. The only guarantee Random() offers
is that setting randseed to particular value will result in the same sequence of
random numbers in a particular version the RTL. A newer version of the RTL may
result in a different sequence for the same randseed.
Errors
None.
See also
Example: ex49
Program Example49;
{ Program to demonstrate the Random and Randomize functions. }
Var I,Count,guess : Longint;
R : Real;
begin
Randomize; { This way we generate a new sequence every time
the program is run}
Count:=0;
For i:=1 to 1000 do
If Random>0.5 then inc(Count);
Writeln ('Generated ',Count,' numbers > 0.5');
Writeln ('out of 1000 generated numbers.');
count:=0;
For i:=1 to 5 do
begin
write ('Guess a number between 1 and 5 : ');
readln(Guess);
If Guess=Random(5)+1 then inc(count);
end;
Writeln ('You guessed ',Count,' out of 5 correct.');
end.An experiment, built from development snapshots. Not the official Free Pascal or Lazarus documentation.
