hedgewars/uRandom.pas
changeset 124 75b892eff74d
parent 107 b08ce0293a51
child 130 19e3c16fb9f0
equal deleted inserted replaced
123:6987769ced3d 124:75b892eff74d
    31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    32  *)
    32  *)
    33 
    33 
    34 unit uRandom;
    34 unit uRandom;
    35 interface
    35 interface
       
    36 {$INCLUDE options.inc}
    36 
    37 
    37 procedure SetRandomSeed(Seed: shortstring);
    38 procedure SetRandomSeed(Seed: shortstring);
    38 function  GetRandom: Double; overload;
    39 function  GetRandom: Double; overload;
    39 function  GetRandom(m: LongWord): LongWord; overload;
    40 function  GetRandom(m: LongWord): LongWord; overload;
    40 
    41 
    41 implementation
    42 implementation
       
    43 uses uMisc;
    42 var cirbuf: array[0..63] of Longword;
    44 var cirbuf: array[0..63] of Longword;
    43     n: byte;
    45     n: byte = 54;
    44 
    46 
    45 function GetNext: Longword;
    47 function GetNext: Longword;
    46 begin
    48 begin
    47 n:= (n + 1) and $3F;
    49 n:= (n + 1) and $3F;
    48 cirbuf[n]:=
    50 cirbuf[n]:=
    54 end;
    56 end;
    55 
    57 
    56 procedure SetRandomSeed(Seed: shortstring);
    58 procedure SetRandomSeed(Seed: shortstring);
    57 var i: Longword;
    59 var i: Longword;
    58 begin
    60 begin
    59 if Length(Seed) > 60 then Seed:= copy(Seed, 1, 60); // not 64 to ensure we have even numbers in cirbuf
    61 if Length(Seed) > 54 then Seed:= copy(Seed, 1, 54); // not 55 to ensure we have odd numbers in cirbuf
       
    62 
    60 for i:= 0 to pred(Length(Seed)) do
    63 for i:= 0 to pred(Length(Seed)) do
    61     cirbuf[i]:= byte(Seed[i + 1]) * 35791253;
    64     cirbuf[i]:= byte(Seed[i + 1]) * (i + 1);
    62 
    65 
    63 for i:= Length(Seed) to 63 do
    66 for i:= Length(Seed) to 54 do
    64     cirbuf[i]:= i * 23860799;
    67     cirbuf[i]:= i * 7 + 1
    65 
       
    66 for i:= 0 to 1024 do GetNext;
       
    67 end;
    68 end;
    68 
    69 
    69 function GetRandom: Double;
    70 function GetRandom: Double;
    70 begin
    71 begin
    71 Result:= frac( GetNext * 0.0007301 + GetNext * 0.003019)
    72 Result:= frac( GetNext * 0.00073 + GetNext * 0.00301)
    72 end;
    73 end;
    73 
    74 
    74 function GetRandom(m: LongWord): LongWord;
    75 function GetRandom(m: LongWord): LongWord;
    75 begin
    76 begin
    76 GetNext;
    77 GetNext;