<--- Turn the page     (contents page)     Turn the page --->


Pascal

Random number generator




This months column is a little about a random number generator. There are very many ways to create random numbers. Some are better and some are worse depending on the application you need. The following source is a simple modulus random number generator.


{ A program from TURBO PASCAL, Walter J. Savitch, 4ed }
{  with a few modifications for ease of writing       }

program rand;

var
  memory, count: integer;

{ This function returns a number between 0 and (modulus-1) }
{  Change modulus below for a different range }
function random(var memory: integer): integer;
  const
    modulus = 729;
    multiplier = 40;
    increment = 3641;

  begin
    memory := (multiplier * memory + increment) mod modulus;
    random := memory;
  end;

begin
  write('Enter a seed: ');
  readln(memory);
  for count := 1 to 10 do
    write(random(memory) : 4 );
  writeln;
end.

¥


<--- Turn the page     (contents page)     Turn the page --->

Page 12