hedgewars/uRandom.pas
branchsdl2transition
changeset 11362 ed5a6478e710
parent 11046 47a8c19ecb60
child 11822 4600dcb9db4f
equal deleted inserted replaced
11361:31570b766315 11362:ed5a6478e710
     1 (*
     1 (*
     2  * Hedgewars, a free turn based strategy game
     2  * Hedgewars, a free turn based strategy game
     3  * Copyright (c) 2004-2013 Andrey Korotaev <unC0Rr@gmail.com>
     3  * Copyright (c) 2004-2015 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  *
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    12  * GNU General Public License for more details.
    13  *
    13  *
    14  * You should have received a copy of the GNU General Public License
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program; if not, write to the Free Software
    15  * along with this program; if not, write to the Free Software
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    17  *)
    17  *)
    18 
    18 
    19 {$INCLUDE "options.inc"}
    19 {$INCLUDE "options.inc"}
    20 
    20 
    21 unit uRandom;
    21 unit uRandom;
    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;