hedgewars/uRandom.pas
changeset 10015 4feced261c68
parent 9998 736015b847e3
parent 9168 20ff80421736
child 10108 c68cf030eded
equal deleted inserted replaced
10014:56d2f2d5aad8 10015:4feced261c68
    29  *)
    29  *)
    30 interface
    30 interface
    31 uses uFloat;
    31 uses uFloat;
    32 
    32 
    33 procedure SetRandomSeed(Seed: shortstring; dropAdditionalPart: boolean); // 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; // 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; 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 
    39 
    39 
    40 implementation
    40 implementation
    43     n: byte;
    43     n: byte;
    44 
    44 
    45 procedure AddRandomness(r: LongWord); inline;
    45 procedure AddRandomness(r: LongWord); inline;
    46 begin
    46 begin
    47 n:= (n + 1) and $3F;
    47 n:= (n + 1) and $3F;
    48 cirbuf[n]:= cirbuf[n] xor r
    48    cirbuf[n]:= cirbuf[n] xor r;
    49 end;
    49 end;
    50 
    50 
    51 function GetNext: Longword; inline;
    51 function GetNext: Longword; inline;
       
    52 var s : string;
    52 begin
    53 begin
    53 n:= (n + 1) and $3F;
    54 n:= (n + 1) and $3F;
    54 cirbuf[n]:=
    55 cirbuf[n]:=
    55            (cirbuf[(n + 40) and $3F] +           {n - 24 mod 64}
    56            (cirbuf[(n + 40) and $3F] +           {n - 24 mod 64}
    56             cirbuf[(n +  9) and $3F])            {n - 55 mod 64}
    57             cirbuf[(n +  9) and $3F])            {n - 55 mod 64}
    57             and $7FFFFFFF;                       {mod 2^31}
    58             and $7FFFFFFF;                       {mod 2^31}
    58 
    59 
    59 GetNext:= cirbuf[n]
    60    GetNext:= cirbuf[n];
       
    61    str(GetNext, s);
    60 end;
    62 end;
    61 
    63 
    62 procedure SetRandomSeed(Seed: shortstring; dropAdditionalPart: boolean);
    64 procedure SetRandomSeed(Seed: shortstring; dropAdditionalPart: boolean);
    63 var i, t, l: Longword;
    65 var i, t, l: Longword;
    64 begin
    66 begin
    78 
    80 
    79 for i:= t to 54 do
    81 for i:= t to 54 do
    80     cirbuf[i]:= $A98765 + 68; // odd number
    82     cirbuf[i]:= $A98765 + 68; // odd number
    81 
    83 
    82 for i:= 0 to 1023 do
    84 for i:= 0 to 1023 do
    83     GetNext
    85    GetNext;
    84 end;
    86 end;
    85 
    87 
    86 function GetRandomf: hwFloat;
    88 function GetRandomf: hwFloat;
    87 begin
    89 begin
    88 GetNext;
    90 GetNext;