hedgewars/uRandom.pas
branchwebgl
changeset 8026 4a4f21070479
parent 7579 51724c98a74f
child 9127 e350500c4edb
equal deleted inserted replaced
8023:7de85783b823 8026:4a4f21070479
    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); // 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; 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 
    39 
    39 
    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);
    64 procedure SetRandomSeed(Seed: shortstring);
    63 var i: Longword;
    65 var i: Longword;
    64 begin
    66 begin
    72 
    74 
    73 for i:= Length(Seed) to 54 do
    75 for i:= Length(Seed) to 54 do
    74     cirbuf[i]:= $A98765 + 68; // odd number
    76     cirbuf[i]:= $A98765 + 68; // odd number
    75 
    77 
    76 for i:= 0 to 1023 do
    78 for i:= 0 to 1023 do
    77     GetNext
    79    GetNext;
    78 end;
    80 end;
    79 
    81 
    80 function GetRandomf: hwFloat;
    82 function GetRandomf: hwFloat;
    81 begin
    83 begin
    82 GetNext;
    84 GetNext;
    83 GetRandomf.isNegative:= false;
    85 GetRandomf.isNegative:= false;
    84 GetRandomf.QWordValue:= GetNext
    86 GetRandomf.QWordValue:= GetNext
    85 end;
    87 end;
    86 
    88 
    87 function GetRandom(m: LongWord): LongWord; inline;
    89 function GetRandom(m: LongWord): LongWord; overload; inline;
    88 begin
    90 begin
    89 GetNext;
    91 GetNext;
    90 GetRandom:= GetNext mod m
    92 GetRandom:= GetNext mod m
    91 end;
    93 end;
    92 
    94