hedgewars/uRandom.pas
changeset 8912 78ea1839aac9
parent 7579 51724c98a74f
child 9080 9b42757d7e71
equal deleted inserted replaced
8911:f17e505ef30b 8912:78ea1839aac9
    28  * a network.
    28  * a network.
    29  *)
    29  *)
    30 interface
    30 interface
    31 uses uFloat;
    31 uses uFloat;
    32 
    32 
    33 procedure SetRandomSeed(Seed: shortstring); // Sets the seed that should be used for generating pseudo-random values.
    33 procedure SetRandomSeed(Seed: shortstring; dropAdditionalPart: boolean); // Sets the seed that should be used for generating pseudo-random values.
    34 function  GetRandomf: hwFloat; overload; // Returns a pseudo-random hwFloat.
    34 function  GetRandomf: hwFloat; overload; // Returns a pseudo-random hwFloat.
    35 function  GetRandom(m: LongWord): LongWord; overload; inline; // Returns a positive pseudo-random integer smaller than m.
    35 function  GetRandom(m: LongWord): LongWord; overload; inline; // Returns a positive pseudo-random integer smaller than m.
    36 procedure AddRandomness(r: LongWord); inline;
    36 procedure AddRandomness(r: LongWord); inline;
    37 function  rndSign(num: hwFloat): hwFloat; // Returns num with a random chance of having a inverted sign.
    37 function  rndSign(num: hwFloat): hwFloat; // Returns num with a random chance of having a inverted sign.
    38 
    38 
    57             and $7FFFFFFF;                       {mod 2^31}
    57             and $7FFFFFFF;                       {mod 2^31}
    58 
    58 
    59 GetNext:= cirbuf[n]
    59 GetNext:= cirbuf[n]
    60 end;
    60 end;
    61 
    61 
    62 procedure SetRandomSeed(Seed: shortstring);
    62 procedure SetRandomSeed(Seed: shortstring; dropAdditionalPart: boolean);
    63 var i: Longword;
    63 var i, t, l: Longword;
    64 begin
    64 begin
    65 n:= 54;
    65 n:= 54;
    66 
    66 
    67 if Length(Seed) > 54 then
    67 if Length(Seed) > 54 then
    68     Seed:= copy(Seed, 1, 54); // not 55 to ensure we have odd numbers in cirbuf
    68     Seed:= copy(Seed, 1, 54); // not 55 to ensure we have odd numbers in cirbuf
    69 
    69 
    70 for i:= 0 to Pred(Length(Seed)) do
    70 t:= 0;
    71     cirbuf[i]:= byte(Seed[i + 1]);
    71 l:= Length(Seed);
    72 
    72 
    73 for i:= Length(Seed) to 54 do
    73 while (t < l) and ((not dropAdditionalPart) or (Seed[t + 1] <> '|')) do
       
    74     begin
       
    75     cirbuf[t]:= byte(Seed[t + 1]);
       
    76     inc(t)
       
    77     end;
       
    78 
       
    79 for i:= t to 54 do
    74     cirbuf[i]:= $A98765 + 68; // odd number
    80     cirbuf[i]:= $A98765 + 68; // odd number
    75 
    81 
    76 for i:= 0 to 1023 do
    82 for i:= 0 to 1023 do
    77     GetNext
    83     GetNext
    78 end;
    84 end;