hedgewars/uRandom.pas
changeset 351 29bc9c36ad5f
parent 320 1ee7f087195a
child 393 db01cc79f278
equal deleted inserted replaced
350:c3ccec3834e8 351:29bc9c36ad5f
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    17  *)
    17  *)
    18 
    18 
    19 unit uRandom;
    19 unit uRandom;
    20 interface
    20 interface
       
    21 uses uFloat;
    21 {$INCLUDE options.inc}
    22 {$INCLUDE options.inc}
    22 
    23 
    23 procedure SetRandomSeed(Seed: shortstring);
    24 procedure SetRandomSeed(Seed: shortstring);
    24 function  GetRandom: Double; overload;
    25 function  GetRandom: hwFloat; overload;
    25 function  GetRandom(m: LongWord): LongWord; overload;
    26 function  GetRandom(m: LongWord): LongWord; overload;
    26 
    27 
    27 implementation
    28 implementation
    28 var cirbuf: array[0..63] of Longword;
    29 var cirbuf: array[0..63] of Longword;
    29     n: byte = 54;
    30     n: byte = 54;
    34 cirbuf[n]:=
    35 cirbuf[n]:=
    35            (cirbuf[(n + 40) and $3F] +           {n - 24 mod 64}
    36            (cirbuf[(n + 40) and $3F] +           {n - 24 mod 64}
    36             cirbuf[(n +  9) and $3F])            {n - 55 mod 64}
    37             cirbuf[(n +  9) and $3F])            {n - 55 mod 64}
    37             and $7FFFFFFF;                       {mod 2^31}
    38             and $7FFFFFFF;                       {mod 2^31}
    38 
    39 
    39 Result:= cirbuf[n]
    40 GetNext:= cirbuf[n]
    40 end;
    41 end;
    41 
    42 
    42 procedure SetRandomSeed(Seed: shortstring);
    43 procedure SetRandomSeed(Seed: shortstring);
    43 var i: Longword;
    44 var i: Longword;
    44 begin
    45 begin
    53     cirbuf[i]:= i * 7 + 1;
    54     cirbuf[i]:= i * 7 + 1;
    54 
    55 
    55 for i:= 0 to 1023 do GetNext
    56 for i:= 0 to 1023 do GetNext
    56 end;
    57 end;
    57 
    58 
    58 function GetRandom: Double;
    59 function GetRandom: hwFloat;
       
    60 var r: hwFloat;
    59 begin
    61 begin
    60 Result:= frac( GetNext * 0.00073 + GetNext * 0.00301)
    62 GetNext;
       
    63 r.isNegative:= false;
       
    64 r.QWordValue:= GetNext;
       
    65 GetRandom:= r
    61 end;
    66 end;
    62 
    67 
    63 function GetRandom(m: LongWord): LongWord;
    68 function GetRandom(m: LongWord): LongWord;
    64 begin
    69 begin
    65 GetNext;
    70 GetNext;
    66 Result:= GetNext mod m
    71 GetRandom:= GetNext mod m
    67 end;
    72 end;
    68 
    73 
    69 end.
    74 end.