Mega Code Archive

 
Categories / Delphi / Examples
 

Randomizing with the linear congruencemethod

Title: Randomizing with the linear congruencemethod Question: The standard randomize function does not return a set of uniformly randomized numbers.How can i truly randomize values? Answer: In order to produce uniformly randomized numbers it is suggested that you use this method rather than using the standard implemented random function. For scientifical purposes this class is more optimal than the standard functions unit realRandomunit; interface uses windows; type realRandom = class private lastResult : integer; public constructor create; function NextInteger : integer; end; const Amplitude = 48271; //these values have been assigned by professionals Modulo = 2147483647; Q = Modulo div Amplitude; R = Modulo mod Amplitude; implementation constructor realRandom.create; var seed : _SYSTEMTIME; begin getSystemTime( seed );//use this for seed lastResult := seed.wMilliseconds; end; function realRandom.NextInteger : integer; begin result := Amplitude * (lastResult mod Q) - R * (lastResult div Q); if result lastResult := result; end; end.