hedgewars/uRandom.pas
branchtransitional_engine
changeset 15929 128ace913837
parent 13358 a79c1206bacd
equal deleted inserted replaced
15928:73cdc306888f 15929:128ace913837
    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; // Returns a pseudo-random hwFloat.
    34 function  GetRandomf: hwFloat; // Returns a pseudo-random hwFloat.
    35 function  GetRandom(m: LongWord): LongWord; inline; // Returns a positive pseudo-random integer smaller than m.
    35 function  GetRandom(m: LongWord): LongWord;  // Returns a positive pseudo-random integer smaller than m.
    36 procedure AddRandomness(r: LongWord); inline;
    36 procedure AddRandomness(r: LongWord); 
    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
    41 
    41 
    42 var cirbuf: array[0..63] of Longword;
    42 var cirbuf: array[0..63] of Longword;
    43     n: byte;
    43     n: byte;
    44 
    44 
    45 procedure AddRandomness(r: LongWord); inline;
    45 procedure AddRandomness(r: LongWord); 
    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; 
    52 begin
    52 begin
    53     n:= (n + 1) and $3F;
    53     n:= (n + 1) and $3F;
    54     cirbuf[n]:=
    54     cirbuf[n]:=
    55            (cirbuf[(n + 40) and $3F] +           {n - 24 mod 64}
    55            (cirbuf[(n + 40) and $3F] +           {n - 24 mod 64}
    56             cirbuf[(n +  9) and $3F])            {n - 55 mod 64}
    56             cirbuf[(n +  9) and $3F])            {n - 55 mod 64}
    88 GetNext;
    88 GetNext;
    89 GetRandomf.isNegative:= false;
    89 GetRandomf.isNegative:= false;
    90 GetRandomf.QWordValue:= GetNext
    90 GetRandomf.QWordValue:= GetNext
    91 end;
    91 end;
    92 
    92 
    93 function GetRandom(m: LongWord): LongWord; inline;
    93 function GetRandom(m: LongWord): LongWord; 
    94 begin
    94 begin
    95 GetNext;
    95 GetNext;
    96 GetRandom:= GetNext mod m
    96 GetRandom:= GetNext mod m
    97 end;
    97 end;
    98 
    98