Skip to content

Commit

Permalink
Bugfix Random* Int64 overloads: Use Trunc
Browse files Browse the repository at this point in the history
  • Loading branch information
ollydev committed Feb 24, 2024
1 parent c748985 commit 2606f6f
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 24 deletions.
105 changes: 105 additions & 0 deletions DocGen/source/tutorials/Random Numbers.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
##############
Random Numbers
##############

Simba provides various methods to generate random numbers with different distributions.

The following methods are:

Random:
"""""""

Random generates approximately uniform distribution.

.. code-block::
function Random: Double; overload;
function Random(l: Int64): Int64; overload;
function Random(min, max: Double): Double; overload;
function Random(min, max: Int64): Int64; overload;
-----

RandomLeft:
"""""""""""

RandomLeft generates weighted numbers towards :code:`Lo`

.. code-block::
function RandomLeft(Lo, Hi: Double): Double; overload;
function RandomLeft(Lo, Hi: Int64): Int64; overload;
-----

RandomRight:
""""""""""""

RandomRight generates weighted numbers towards :code:`Hi`

.. code-block::
function RandomRight(Lo, Hi: Double): Double; overload;
function RandomRight(Lo, Hi: Int64): Int64; overload;
-----

RandomMean:
"""""""""""

RandomMean generates weighted numbers towards the mean of :code:`Lo..Hi`

.. code-block::
function RandomMean(Lo, Hi: Double): Double; overload;
function RandomMean(Lo, Hi: Int64): Int64; overload;
-----

RandomMode:
"""""""""""

RandomMode generates weighted numbers towards :code:`Mode` within :code:`Lo..Hi`

.. code-block::
function RandomMode(Mode, Lo, Hi: Double): Double; overload;
function RandomMode(Mode, Lo, Hi: Int64): Int64; overload;
-----

| You can also write a small script to show the spread.
| This shows the spread of 1000000 calls of :code:`RandomLeft(0, 10)`:
.. code-block::
const
SampleCount = 1000000;
Range = 10;
var
Hits: TIntegerArray;
I: Integer;
begin
SetLength(Hits, Range);
for I := 1 to SampleCount do
Hits[RandomLeft(0, Range)] += 1;
for I := 0 to High(Hits) do
WriteLn(I, ' was rolled ', Hits[I], ' times (', Round((Hits[I] / SampleCount) * 100, 4), ' percent)');
end;
Output

.. code-block:: none
0 was rolled 382874 times (38.2874 percent)
1 was rolled 300305 times (30.0305 percent)
2 was rolled 183102 times (18.3102 percent)
3 was rolled 88345 times (8.8345 percent)
4 was rolled 32958 times (3.2958 percent)
5 was rolled 9715 times (0.9715 percent)
6 was rolled 2195 times (0.2195 percent)
7 was rolled 442 times (0.0442 percent)
8 was rolled 60 times (0.006 percent)
9 was rolled 4 times (0.0004 percent)
1 change: 1 addition & 0 deletions DocGen/source/tutorials/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Tutorials

.. toctree::

Random Numbers.rst
Color Finding.rst
Sleep Until.rst
Input & Finder Target.rst
Expand Down
2 changes: 1 addition & 1 deletion DocGen/source/tutorials/plugins/plugin-cpp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ main.h
void (*SetArrayLength)(void* TypeInfo, void** var, std::size_t new_len);
std::size_t (*GetArrayLength)(void* array);

void* (*ExternalImage_Create)(bool FreeOnTerminate);
void* (*ExternalImage_Create)(bool AutoResize);
void (*ExternalImage_SetMemory)(void* img, void* bgra_data, std::int32_t width, std::int32_t height);
void (*ExternalImage_Resize)(void* img, std::int32_t new_width, std::int32_t new_height);
void (*ExternalImage_SetUserData)(void* img, void* userdata);
Expand Down
2 changes: 1 addition & 1 deletion DocGen/source/tutorials/plugins/writing-plugins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ These pointers are provided to these structures:
SetArrayLength: procedure(TypeInfo: Pointer; var AVar: Pointer; NewLen: NativeInt); cdecl;
GetArrayLength: function(AVar: Pointer): NativeInt; cdecl;
ExternalImage_Create: function(FreeOnTerminate: Boolean): Pointer; cdecl;
ExternalImage_Create: function(AutoResize: Boolean): Pointer; cdecl;
ExternalImage_SetMemory: procedure(Img: Pointer; Data: PColorBGRA; AWidth, AHeight: Integer); cdecl;
ExternalImage_Resize: procedure(Img: Pointer; NewWidth, NewHeight: Integer); cdecl;
ExternalImage_SetUserData: procedure(Img: Pointer; UserData: Pointer); cdecl;
Expand Down
15 changes: 15 additions & 0 deletions Examples/randomleft.simba
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const
SampleCount = 1000000;
Range = 10;

var
Hits: TIntegerArray;
I: Integer;
begin
SetLength(Hits, Range);
for I := 1 to SampleCount do
Hits[RandomLeft(0, Range)] += 1;

for I := 0 to High(Hits) do
WriteLn(I, ' was rolled ', Hits[I], ' times (', Round((Hits[I] / SampleCount) * 100, 4), ' percent)');
end;
3 changes: 2 additions & 1 deletion Source/Simba.lpi
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<TextDesc Value=""/>
</XPManifest>
<Icon Value="0"/>
<Resources Count="13">
<Resources Count="14">
<Resource_0 FileName="..\Examples\array.simba" Type="RCDATA" ResourceName="EXAMPLE_ARRAY"/>
<Resource_1 FileName="..\Examples\function.simba" Type="RCDATA" ResourceName="EXAMPLE_FUNCTION"/>
<Resource_2 FileName="..\Examples\loop.simba" Type="RCDATA" ResourceName="EXAMPLE_LOOP"/>
Expand All @@ -31,6 +31,7 @@
<Resource_10 FileName="..\Examples\irc.simba" Type="RCDATA" ResourceName="EXAMPLE_IRC"/>
<Resource_11 FileName="..\Examples\image_drawtext.simba" Type="RCDATA" ResourceName="EXAMPLE_DRAWTEXT"/>
<Resource_12 FileName="..\Examples\form_imagebox.simba" Type="RCDATA" ResourceName="EXAMPLE_IMAGEBOX"/>
<Resource_13 FileName="..\Examples\randomleft.simba" Type="RCDATA" ResourceName="EXAMPLE_RANDOMLEFT"/>
</Resources>
</General>
<VersionInfo>
Expand Down
Binary file modified Source/Simba.res
Binary file not shown.
2 changes: 1 addition & 1 deletion Source/forms/simba.openexampleform.lfm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ object SimbaOpenExampleForm: TSimbaOpenExampleForm
OnCreate = FormCreate
OnShow = FormShow
Position = poMainFormCenter
LCLVersion = '3.0.0.2'
LCLVersion = '3.0.0.3'
object LeftPanel: TPanel
Left = 0
Height = 750
Expand Down
1 change: 1 addition & 0 deletions Source/forms/simba.openexampleform.pas
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ procedure TSimbaOpenExampleForm.FormCreate(Sender: TObject);
AddNode('Image Box', 'EXAMPLE_IMAGEBOX' );
AddNode('IRC', 'EXAMPLE_IRC' );
AddNode('Draw Text', 'EXAMPLE_DRAWTEXT' );
AddNode('RandomLeft', 'EXAMPLE_RANDOMLEFT' );
end;

procedure TSimbaOpenExampleForm.DoTreeViewSelectionChanged(Sender: TObject);
Expand Down
41 changes: 23 additions & 18 deletions Source/simba.random.pas
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ interface
function RandomCenterTPA(Amount: Integer; Box: TBox): TPointArray;
function RandomTPA(Amount: Integer; Box: TBox): TPointArray;

function RandomRange(Lo, Hi: Double): Double; overload;
function Random(Lo, Hi: Double): Double; overload;
function Random(Lo, Hi: Int64): Int64; overload;

function RandomLeft(Lo, Hi: Double): Double; overload;
function RandomLeft(Lo, Hi: Int64): Int64; overload;
Expand All @@ -40,8 +41,7 @@ procedure BetterRandomize;
implementation

uses
Math, DateUtils,
simba.box;
Math, DateUtils;

function nzRandom: Double;
begin
Expand Down Expand Up @@ -81,11 +81,16 @@ function RandomTPA(Amount: Integer; Box: TBox): TPointArray;
Result[i] := Point(RandomRange(Box.X1, Box.X2), RandomRange(Box.Y1, Box.Y2));
end;

function RandomRange(Lo, Hi: Double): Double;
function Random(Lo, Hi: Double): Double;
begin
Result := Lo + Random() * (Hi - Lo);
end;

function Random(Lo, Hi: Int64): Int64;
begin
Result := Trunc(Random(Lo, Hi));
end;

function RandomLeft(Lo, Hi: Double): Double;
begin
Result := RandCutoff + 1;
Expand All @@ -96,7 +101,7 @@ function RandomLeft(Lo, Hi: Double): Double;

function RandomLeft(Lo, Hi: Int64): Int64;
begin
Result := Round(RandomLeft(Lo * 1.00, Hi * 1.00));
Result := Trunc(RandomLeft(Lo * 1.00, Hi * 1.00));
end;

function RandomRight(Lo, Hi: Double): Double;
Expand All @@ -106,20 +111,20 @@ function RandomRight(Lo, Hi: Double): Double;

function RandomRight(Lo, Hi: Int64): Int64;
begin
Result := Round(RandomRight(Lo * 1.00, Hi * 1.00));
Result := Trunc(RandomRight(Lo * 1.00, Hi * 1.00));
end;

function RandomMean(Lo, Hi: Double): Double;
begin
case Random(2) of
0: Result := (Hi+Lo) / 2.0 + RandomLeft(0, (Hi-Lo) / 2);
1: Result := (Hi+Lo) / 2.0 - RandomLeft(0, (Hi-Lo) / 2);
end;
if (Random() < 0.50) then
Result := (Hi+Lo) / 2.0 + RandomLeft(0, (Hi-Lo) / 2)
else
Result := (Hi+Lo) / 2.0 - RandomLeft(0, (Hi-Lo) / 2);
end;

function RandomMean(Lo, Hi: Int64): Int64;
begin
Result := Round(RandomMean(Lo * 1.00, Hi * 1.00));
Result := Trunc(RandomMean(Lo * 1.00, Hi * 1.00));
end;

function RandomMode(Mode, Lo, Hi: Double): Double;
Expand All @@ -138,7 +143,7 @@ function RandomMode(Mode, Lo, Hi: Double): Double;

function RandomMode(Mode, Lo, Hi: Int64): Int64;
begin
Result := Round(RandomMode(Mode * 1.00, Lo * 1.00, Hi * 1.00));
Result := Trunc(RandomMode(Mode * 1.00, Lo * 1.00, Hi * 1.00));
end;

function GaussRand(Mean, Dev: Double): Double;
Expand All @@ -156,12 +161,12 @@ procedure BetterRandomize; // https://github.com/dajobe/libmtwist/blob/master/se

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;
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
Expand Down
4 changes: 2 additions & 2 deletions Source/simba.target.pas
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ TTargetMethods = record
function GetImageData(var ABounds: TBox; var Data: PColorBGRA; var DataWidth: Integer): Boolean;
procedure FreeImageData(var Data: PColorBGRA);

function IsImageFrozen: Boolean; inline;
function IsImageFrozen: Boolean;
procedure FreezeImage(ABounds: TBox);
procedure UnFreezeImage;

Expand Down Expand Up @@ -645,7 +645,7 @@ function TSimbaTarget.ToString: String;
ETargetType.EIOS:
Result := 'ETargetType.EIOS: Target=%P'.Format([FTargetEIOS.Target]);
ETargetType.PLUGIN:
Result := 'ETargetType.PLUGIN: Filename="%s" Target=%P'.Format([FTargetPlugin.FileName, FTargetPlugin.Target]);
Result := 'ETargetType.PLUGIN: Filename="%s", Target=%P'.Format([FTargetPlugin.FileName, FTargetPlugin.Target]);
end;
end;

Expand Down
2 changes: 2 additions & 0 deletions Source/targets/simba.target_plugin.pas
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ constructor TUpdateDebugImageThread.Create(Target: TSimbaPluginTarget; Img: TSim
begin
inherited Create(False, 512*512);

FreeOnTerminate := True;

FTarget := Target;
FImg := Img;
end;
Expand Down

0 comments on commit 2606f6f

Please sign in to comment.