Skip to content

Commit

Permalink
Better Randomize
Browse files Browse the repository at this point in the history
  • Loading branch information
ollydev committed Aug 5, 2023
1 parent c728542 commit d286bea
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
16 changes: 15 additions & 1 deletion Source/script/imports/simba/simba.import_random.pas
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ procedure _LapeRandomModeI(const Params: PParamArray; const Result: Pointer); LA
(*
GaussRand
~~~~~~~~~
> function GaussRand(Mean, Dev: Double): Double
> function GaussRand(Mean, Dev: Double): Double;
Generates a random gaussian/normal number.
*)
Expand All @@ -160,6 +160,18 @@ procedure _LapeGaussRand(const Params: PParamArray; const Result: Pointer); LAPE
PDouble(Result)^ := GaussRand(PDouble(Params^[0])^, PDouble(Params^[1])^);
end;

(*
Randomize
~~~~~~~~~
> procedure Randomize;
Generates a new `RandSeed`
*)
procedure _LapeRandomize(const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV
begin
BetterRandomize();
end;

procedure ImportRandom(Compiler: TSimbaScript_Compiler);
begin
with Compiler do
Expand All @@ -168,6 +180,8 @@ procedure ImportRandom(Compiler: TSimbaScript_Compiler);

addGlobalVar(ltDouble, @RandCutoff, 'RandCutoff');

addGlobalFunc('procedure Randomize; override;', @_LapeRandomize);

addGlobalFunc('function RandomCenterTPA(Amount: Integer; Box: TBox): TPointArray', @_LapeRandomCenterTPA);
addGlobalFunc('function RandomTPA(Amount: Integer; Box: TBox): TPointArray', @_LapeRandomTPA);

Expand Down
7 changes: 4 additions & 3 deletions Source/simba.init.pas
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ interface
{$IFDEF UNIX}
cthreads,
{$ENDIF}
classes, sysutils, lazlogger;
Classes, SysUtils,
simba.random;

implementation

Expand Down Expand Up @@ -61,13 +62,13 @@ initialization
LinuxInitialization();
{$ENDIF}

Randomize();

FormatSettings.ThousandSeparator := ',';
FormatSettings.DecimalSeparator := '.';
FormatSettings.DateSeparator := '-';
FormatSettings.TimeSeparator := ':';

BetterRandomize();

finalization
{$IFDEF DARWIN}
DarwinFinalization();
Expand Down
34 changes: 32 additions & 2 deletions Source/simba.random.pas
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
interface

uses
classes, sysutils,
Classes, SysUtils,
simba.mufasatypes;

function RandomCenterTPA(Amount: Integer; Box: TBox): TPointArray;
Expand All @@ -30,13 +30,16 @@ function RandomMode(Mode, Lo, Hi: Int64): Int64; overload;

function GaussRand(Mean, Dev: Double): Double;

procedure BetterRandomize;

var
RandCutoff: Double = 5;

implementation

uses
math;
Math,
simba.process;

function nzRandom: Double;
begin
Expand Down Expand Up @@ -139,5 +142,32 @@ function GaussRand(Mean, Dev: Double): Double;
Result := Mean + Len * Cos(2 * PI * Random());
end;

{$R-}{$Q-}

// https://github.com/dajobe/libmtwist/blob/master/seed.c
procedure BetterRandomize;

procedure Mix(var A, B, C: UInt32);
begin
A -= C; A := A xor ((C shl 4) or (C shr (32-4))); C += B;
B -= A; B := B xor ((A shl 6) or (A shr (32-6))); A += C;
C -= B; C := C xor ((B shl 8) or (B shr (32-8))); B += A;
A -= C; A := A xor ((C shl 16) or (C shr (32-16))); C += B;
B -= A; B := B xor ((A shl 19) or (A shr (32-19))); A += C;
C -= B; C := C xor ((B shl 4) or (B shr (32-4))); B += A;
end;

var
A, B, C: UInt32;
begin
A := UInt32(GetTickCount64());
B := UInt32(GetProcessID());
C := UInt32(SimbaProcess.GetProcessRunnningTime(GetProcessID()));

Mix(A, B, C);

RandSeed := C;
end;

end.

0 comments on commit d286bea

Please sign in to comment.