DelphiBasics
Random
Function
Generate a random floating point or integer number System unit
1 function Random():Extended;
2 function Random (LimitPlusOne Integer:Integer;
Description
The Random function generates random numbers. They can be floating point numbers in the range :
 
0 <= Number < 1.0
 
or integer numbers in the range :
 
0 <= Number < LimitPlusOne
 
Delphi uses a pseudo random number generator that always returns the same sequence of values (232) each time the program runs.
 
To avoid this predictability, use the Randomize procedure. It repositions into this random number sequence using the time of day as a pseudo random seed.
Related commands
RandomizeReposition the Random number generator next value
RandomRangeGenerate a random integer number within a supplied range
RandSeedReposition the Random number generator next value
 Download this web site as a Windows program.




 
Example code : Generate sets of floating point and integer numbers
var
  float : single;
  int   : Integer;
  i     : Integer;

begin
  // Get floating point random numbers in the range 0 <= Number < 1.0
  for i := 1 to 5 do
  begin
    float := Random;
    ShowMessage('float = '+FloatToStr(float));
  end;

  ShowMessage('');

  // Get an integer random number in the range 1..100
  for i := 1 to 5 do
  begin
    int := 1 + Random(100);    // The 100 value gives a range 0..99
    ShowMessage('int = '+IntToStr(int));
  end;
end;
Show full unit code
  float = 2.3283064365387E-10
  float = 0.031379981256104
  float = 0.861048460006714
  float = 0.202580958604813
  float = 0.2729212641716
  
  
  int = 68
  int = 32
  int = 17
  int = 38
  int = 43
 
Delphi Programming © Neil Moffatt . All rights reserved.  |  Home Page