hedgewars/uRandom.pas
changeset 130 19e3c16fb9f0
parent 124 75b892eff74d
child 136 89970b70b076
equal deleted inserted replaced
129:313f3d0ee7fa 130:19e3c16fb9f0
    38 procedure SetRandomSeed(Seed: shortstring);
    38 procedure SetRandomSeed(Seed: shortstring);
    39 function  GetRandom: Double; overload;
    39 function  GetRandom: Double; overload;
    40 function  GetRandom(m: LongWord): LongWord; overload;
    40 function  GetRandom(m: LongWord): LongWord; overload;
    41 
    41 
    42 implementation
    42 implementation
    43 uses uMisc;
       
    44 var cirbuf: array[0..63] of Longword;
    43 var cirbuf: array[0..63] of Longword;
    45     n: byte = 54;
    44     n: byte = 54;
    46 
    45 
    47 function GetNext: Longword;
    46 function GetNext: Longword;
    48 begin
    47 begin
    49 n:= (n + 1) and $3F;
    48 n:= (n + 1) and $3F;
    50 cirbuf[n]:=
    49 cirbuf[n]:=
    51            (cirbuf[(n + 40) and $3F] +           {n - 24 mod 64}
    50            (cirbuf[(n + 40) and $3F] +           {n - 24 mod 64}
    52             cirbuf[(n +  9) and $3F])            {n - 55 mod 64}
    51             cirbuf[(n +  9) and $3F])            {n - 55 mod 64}
    53             and $7FFFFFFF;                       {mod 2^31}
    52             and $7FFFFFFF;                       {mod 2^31}
    54             
       
    55 Result:= cirbuf[n]
    53 Result:= cirbuf[n]
    56 end;
    54 end;
    57 
    55 
    58 procedure SetRandomSeed(Seed: shortstring);
    56 procedure SetRandomSeed(Seed: shortstring);
    59 var i: Longword;
    57 var i: Longword;
    62 
    60 
    63 for i:= 0 to pred(Length(Seed)) do
    61 for i:= 0 to pred(Length(Seed)) do
    64     cirbuf[i]:= byte(Seed[i + 1]) * (i + 1);
    62     cirbuf[i]:= byte(Seed[i + 1]) * (i + 1);
    65 
    63 
    66 for i:= Length(Seed) to 54 do
    64 for i:= Length(Seed) to 54 do
    67     cirbuf[i]:= i * 7 + 1
    65     cirbuf[i]:= i * 7 + 1;
       
    66 
       
    67 for i:= 0 to 1023 do GetNext
    68 end;
    68 end;
    69 
    69 
    70 function GetRandom: Double;
    70 function GetRandom: Double;
    71 begin
    71 begin
    72 Result:= frac( GetNext * 0.00073 + GetNext * 0.00301)
    72 Result:= frac( GetNext * 0.00073 + GetNext * 0.00301)