hedgewars/uRandom.pas
changeset 105 e7cb9bb4a9de
parent 102 c45643d3fd78
child 107 b08ce0293a51
--- a/hedgewars/uRandom.pas	Thu Aug 10 21:22:55 2006 +0000
+++ b/hedgewars/uRandom.pas	Thu Aug 10 21:52:14 2006 +0000
@@ -39,7 +39,6 @@
 function  GetRandom(m: LongWord): LongWord; overload;
 
 implementation
-const rndM = 2147483578;
 var cirbuf: array[0..63] of Longword;
     n: byte;
 
@@ -47,8 +46,9 @@
 begin
 n:= (n + 1) and $3F;
 cirbuf[n]:=
-           (cirbuf[(n + 40) and $3F] +           {== n - 24 mod 64}
-            cirbuf[(n +  9) and $3F]) mod rndM;  {== n - 55 mod 64}
+           (cirbuf[(n + 40) and $3F] +           {n - 24 mod 64}
+            cirbuf[(n +  9) and $3F])            {n - 55 mod 64}
+            and $7FFFFFFF;                       {mod 2^31}
             
 Result:= cirbuf[n]
 end;
@@ -56,6 +56,7 @@
 procedure SetRandomSeed(Seed: shortstring);
 var i: Longword;
 begin
+if Length(Seed) > 60 then Seed:= copy(Seed, 1, 60); // not 64 to ensure we have even numbers in cirbuf
 for i:= 0 to pred(Length(Seed)) do
     cirbuf[i]:= byte(Seed[i + 1]) * 35791253;
 
@@ -72,6 +73,7 @@
 
 function GetRandom(m: LongWord): LongWord;
 begin
+GetNext;
 Result:= GetNext mod m
 end;