hedgewars/uRandom.pas
branchwebgl
changeset 9127 e350500c4edb
parent 8026 4a4f21070479
parent 9080 9b42757d7e71
child 9168 20ff80421736
equal deleted inserted replaced
8860:bde641cf53c8 9127:e350500c4edb
     1 (*
     1 (*
     2  * Hedgewars, a free turn based strategy game
     2  * Hedgewars, a free turn based strategy game
     3  * Copyright (c) 2004-2012 Andrey Korotaev <unC0Rr@gmail.com>
     3  * Copyright (c) 2004-2013 Andrey Korotaev <unC0Rr@gmail.com>
     4  *
     4  *
     5  * This program is free software; you can redistribute it and/or modify
     5  * This program is free software; you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation; version 2 of the License
     7  * the Free Software Foundation; version 2 of the License
     8  *
     8  *
    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; // 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 
    39 
    39 
    59 
    59 
    60    GetNext:= cirbuf[n];
    60    GetNext:= cirbuf[n];
    61    str(GetNext, s);
    61    str(GetNext, s);
    62 end;
    62 end;
    63 
    63 
    64 procedure SetRandomSeed(Seed: shortstring);
    64 procedure SetRandomSeed(Seed: shortstring; dropAdditionalPart: boolean);
    65 var i: Longword;
    65 var i, t, l: Longword;
    66 begin
    66 begin
    67 n:= 54;
    67 n:= 54;
    68 
    68 
    69 if Length(Seed) > 54 then
    69 if Length(Seed) > 54 then
    70     Seed:= copy(Seed, 1, 54); // not 55 to ensure we have odd numbers in cirbuf
    70     Seed:= copy(Seed, 1, 54); // not 55 to ensure we have odd numbers in cirbuf
    71 
    71 
    72 for i:= 0 to Pred(Length(Seed)) do
    72 t:= 0;
    73     cirbuf[i]:= byte(Seed[i + 1]);
    73 l:= Length(Seed);
    74 
    74 
    75 for i:= Length(Seed) to 54 do
    75 while (t < l) and ((not dropAdditionalPart) or (Seed[t + 1] <> '|')) do
       
    76     begin
       
    77     cirbuf[t]:= byte(Seed[t + 1]);
       
    78     inc(t)
       
    79     end;
       
    80 
       
    81 for i:= t to 54 do
    76     cirbuf[i]:= $A98765 + 68; // odd number
    82     cirbuf[i]:= $A98765 + 68; // odd number
    77 
    83 
    78 for i:= 0 to 1023 do
    84 for i:= 0 to 1023 do
    79    GetNext;
    85    GetNext;
    80 end;
    86 end;