SmartPascal
RandSeed
Variable
Reposition the Random number generator next value System unit
  var RandSeed : LongInt;
Description
The RandSeed variable is used in conjunction with the Random function. It changes the seed used that Delphi uses to generate its range of 232 pseudo random numbers.
Related commands
Random Generate a random floating point or integer number
Randomize Reposition the Random number generator next value
RandomRange Generate a random integer number within a supplied range
 
Example code : Run this code twice to see the effect of RandSeed
// 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
  int   : Integer;
  i     : Integer;
  hours, mins, secs, milliSecs : Word;

begin
  // Get random numbers using the default random seed value
  ShowMessage('Fixed first 5 random numbers');
  for i := 1 to 5 do
  begin
    int := 1 + Random(100);    // The 100 value gives a range 0..99
    ShowMessage('int = '+IntToStr(int));
  end;

  // Now change the random seed to the milliseconds value
  // of the current time
  DecodeTime(now, hours, mins, secs, milliSecs);
  ShowMessage('');
  ShowMessage('Setting randSeed to value : '+IntToStr(milliSecs));
  ShowMessage('');
  RandSeed := milliSecs;

  // Get an integer random number in the range 1..100
  ShowMessage('Random next 5 numbers');
  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
   Fixed first 5 random numbers
   int = 1
   int = 4
   int = 87
   int = 21
   int = 28
  
   Setting random seed to value : 660
  
   Random next 5 numbers
   int = 72
   int = 62
   int = 28
   int = 63
   int = 44