SmartPascal
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
Randomize Reposition the Random number generator next value
RandomRange Generate a random integer number within a supplied range
RandSeed Reposition the Random number generator next value
 
Example code : Generate sets of floating point and integer numbers
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
 
unit Unit1;
 
interface
 
uses
  // The System unit does not need to be defined
  SysUtils,
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

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;
 
end.
Hide 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