|
@@ -51,8 +51,6 @@ const
|
|
{$else}
|
|
{$else}
|
|
STACK_MARGIN = 16384; { Stack size margin for stack checking }
|
|
STACK_MARGIN = 16384; { Stack size margin for stack checking }
|
|
{$endif}
|
|
{$endif}
|
|
-{ Random / Randomize constants }
|
|
|
|
- OldRandSeed : Cardinal = 0;
|
|
|
|
|
|
|
|
{ For Error Handling.}
|
|
{ For Error Handling.}
|
|
ErrorBase : Pointer = nil;public name 'FPC_ERRORBASE';
|
|
ErrorBase : Pointer = nil;public name 'FPC_ERRORBASE';
|
|
@@ -603,29 +601,12 @@ type
|
|
{$R-} {range checking off}
|
|
{$R-} {range checking off}
|
|
{$Q-} {overflow checking off}
|
|
{$Q-} {overflow checking off}
|
|
|
|
|
|
-const
|
|
|
|
- MTWIST_N = 624;
|
|
|
|
- MTWIST_M = 397;
|
|
|
|
-
|
|
|
|
- MT_STATIC_SEED = 5489;
|
|
|
|
-
|
|
|
|
- MTWIST_UPPER_MASK = cardinal($80000000);
|
|
|
|
- MTWIST_LOWER_MASK = cardinal($7FFFFFFF);
|
|
|
|
-
|
|
|
|
- MTWIST_MATRIX_A = cardinal($9908B0DF);
|
|
|
|
-
|
|
|
|
-var
|
|
|
|
- mt_state: array[0..MTWIST_N-1] of cardinal;
|
|
|
|
-
|
|
|
|
-const
|
|
|
|
- mt_index: cardinal = MTWIST_N+1;
|
|
|
|
-
|
|
|
|
-function MTWIST_MIXBITS(u, v: cardinal): cardinal; inline;
|
|
|
|
|
|
+function TRandomGenerator.MTWIST_MIXBITS(u, v: cardinal): cardinal; inline;
|
|
begin
|
|
begin
|
|
result:=(u and MTWIST_UPPER_MASK) or (v and MTWIST_LOWER_MASK);
|
|
result:=(u and MTWIST_UPPER_MASK) or (v and MTWIST_LOWER_MASK);
|
|
end;
|
|
end;
|
|
|
|
|
|
-function MTWIST_TWIST(u, v: cardinal): cardinal; inline;
|
|
|
|
|
|
+function TRandomGenerator.MTWIST_TWIST(u, v: cardinal): cardinal; inline;
|
|
begin
|
|
begin
|
|
{ the construct at the end is equivalent to
|
|
{ the construct at the end is equivalent to
|
|
if odd(v) then
|
|
if odd(v) then
|
|
@@ -636,7 +617,7 @@ begin
|
|
result:=(MTWIST_MIXBITS(u,v) shr 1) xor (cardinal(-(v and 1)) and MTWIST_MATRIX_A);
|
|
result:=(MTWIST_MIXBITS(u,v) shr 1) xor (cardinal(-(v and 1)) and MTWIST_MATRIX_A);
|
|
end;
|
|
end;
|
|
|
|
|
|
-procedure mtwist_init(seed: cardinal);
|
|
|
|
|
|
+procedure TRandomGenerator.mtwist_init(seed: cardinal);
|
|
var
|
|
var
|
|
i: longint;
|
|
i: longint;
|
|
begin
|
|
begin
|
|
@@ -647,7 +628,7 @@ begin
|
|
mt_index:=MTWIST_N;
|
|
mt_index:=MTWIST_N;
|
|
end;
|
|
end;
|
|
|
|
|
|
-procedure mtwist_update_state;
|
|
|
|
|
|
+procedure TRandomGenerator.mtwist_update_state;
|
|
var
|
|
var
|
|
count: longint;
|
|
count: longint;
|
|
begin
|
|
begin
|
|
@@ -668,7 +649,7 @@ begin
|
|
end;
|
|
end;
|
|
|
|
|
|
|
|
|
|
-function mtwist_u32rand: cardinal;
|
|
|
|
|
|
+function TRandomGenerator.mtwist_u32rand: cardinal;
|
|
var
|
|
var
|
|
l_index :cardinal;
|
|
l_index :cardinal;
|
|
begin
|
|
begin
|
|
@@ -702,7 +683,7 @@ begin
|
|
end;
|
|
end;
|
|
|
|
|
|
|
|
|
|
-function random(l:longint): longint;
|
|
|
|
|
|
+function TRandomGenerator.Random(l:longint): longint;
|
|
begin
|
|
begin
|
|
{ otherwise we can return values = l (JM) }
|
|
{ otherwise we can return values = l (JM) }
|
|
if (l < 0) then
|
|
if (l < 0) then
|
|
@@ -710,7 +691,7 @@ begin
|
|
random := longint((int64(mtwist_u32rand)*l) shr 32);
|
|
random := longint((int64(mtwist_u32rand)*l) shr 32);
|
|
end;
|
|
end;
|
|
|
|
|
|
-function random(l:int64): int64;
|
|
|
|
|
|
+function TRandomGenerator.Random(l:int64): int64;
|
|
var
|
|
var
|
|
a, b, c, d: cardinal;
|
|
a, b, c, d: cardinal;
|
|
q, bd, ad, bc, ac: qword;
|
|
q, bd, ad, bc, ac: qword;
|
|
@@ -745,12 +726,49 @@ begin
|
|
end;
|
|
end;
|
|
|
|
|
|
{$ifndef FPUNONE}
|
|
{$ifndef FPUNONE}
|
|
-function random: extended;
|
|
|
|
|
|
+function TRandomGenerator.Random: extended;
|
|
begin
|
|
begin
|
|
random := mtwist_u32rand * (extended(1.0)/(int64(1) shl 32));
|
|
random := mtwist_u32rand * (extended(1.0)/(int64(1) shl 32));
|
|
end;
|
|
end;
|
|
{$endif}
|
|
{$endif}
|
|
|
|
|
|
|
|
+procedure TRandomGenerator.Randomize(ARandSeed: cardinal);
|
|
|
|
+begin
|
|
|
|
+ mt_index:=MTWIST_N+1;
|
|
|
|
+ OldRandSeed:=0;
|
|
|
|
+ Self.RandSeed:=ARandSeed;
|
|
|
|
+end;
|
|
|
|
+
|
|
|
|
+procedure TRandomGenerator.Randomize;
|
|
|
|
+begin
|
|
|
|
+ mt_index:=MTWIST_N+1;
|
|
|
|
+ OldRandSeed:=0;
|
|
|
|
+ System.Randomize(Self.RandSeed);
|
|
|
|
+end;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+function random(l:longint): longint;
|
|
|
|
+begin
|
|
|
|
+ Result:=RandGenerator.Random(l);
|
|
|
|
+end;
|
|
|
|
+
|
|
|
|
+function random(l:int64): int64;
|
|
|
|
+begin
|
|
|
|
+ Result:=RandGenerator.Random(l);
|
|
|
|
+end;
|
|
|
|
+
|
|
|
|
+{$ifndef FPUNONE}
|
|
|
|
+function random: extended;
|
|
|
|
+begin
|
|
|
|
+ Result:=RandGenerator.Random;
|
|
|
|
+end;
|
|
|
|
+{$endif}
|
|
|
|
+
|
|
|
|
+Procedure Randomize;
|
|
|
|
+begin
|
|
|
|
+ RandGenerator.Randomize;
|
|
|
|
+end;
|
|
|
|
+
|
|
{$else FPC_USE_SIMPLE_RANDOM}
|
|
{$else FPC_USE_SIMPLE_RANDOM}
|
|
|
|
|
|
{ A simple implementation of random. TP/Delphi compatible. }
|
|
{ A simple implementation of random. TP/Delphi compatible. }
|