forked from Villavu/SRL-Development
/
random.simba
331 lines (282 loc) · 9.9 KB
/
random.simba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
(*
Random
=======
Random related methods.
*)
{$DEFINE SRL_RANDOM_INCLUDED}
{$IFNDEF SRL_UTILS}
{$I SRL/utils.simba}
{$ENDIF}
type
EWaitDir = (wdLeft,wdMean,wdRight);
var
SRL_GAUSS_CUTOFF: Double = 4.0;
//none-zero random
function nzRandom(): Extended;
begin
if SizeOf(Extended) = 10 then
Result := Max(Random(), 1.0e-4900) //10^-4900 seems to be our safe limit
else
Result := Max(Random(), 1.0e-320);
end;
(*
SRL.GaussRand
~~~~~~~~~~~~~
.. pascal:: function TSRL.GaussRand(Mean, Dev: Double): Double;
Generates a random gaussian/normal number.
*)
function TSRL.GaussRand(Mean, Dev: Double): Double; static;
var
len: Double;
begin
len := Dev * Sqrt(-2 * Ln(nzRandom()));
Result := Mean + len * Cos(2 * PI * Random());
end;
(*
SRL.TruncatedGauss
~~~~~~~~~~~~~~~~~~
.. pascal:: function TSRL.TruncatedGauss(Left:Double=0; Right:Double=1): Double;
Generates a random gaussian/normal number which is truncated and mapped within then
given range ``[left..right]`` weighted towards ``left``
*)
function TSRL.TruncatedGauss(Left:Double=0; Right:Double=1; CUTOFF:Single=0): Double; static; overload;
begin
if CUTOFF <= 0 then CUTOFF := SRL_GAUSS_CUTOFF;
Result := CUTOFF+1;
while Result >= CUTOFF do
Result := Abs(Sqrt(-2 * Ln(nzRandom())) * Cos(2 * PI * Random()));
Result := Result / CUTOFF * (Right-Left) + Left;
end;
(*
SRL.TruncatedGauss
~~~~~~~~~~~~~~~~~~
.. pascal function TSRL.TruncatedGauss(Left:Int64=0; Right:Int64=1; CUTOFF:Single=0): Int64;
Generates a random gaussian/normal number which is truncated and mapped within then
given range ``[left..right]`` weighted towards ``left``
*)
function TSRL.TruncatedGauss(Left:Int64=0; Right:Int64=1; CUTOFF:Single=0): Int64; static; overload;
begin
Result := Round(SRL.TruncatedGauss(Left * 1.00, Right * 1.00, CUTOFF));
end;
(*
SRL.SkewedRand
~~~~~~~~~~~~~~~
.. pascal:: function TSRL.SkewedRand(Mode, Lo, Hi: Double): Double; static;
Random skewed distribution generation. `Mode` is a number between `Lo` and `Hi` which is
where the most of the generated numbers will land.
*)
function TSRL.SkewedRand(Mode, Lo, Hi: Double; CUTOFF:Single=0): Double; static;
var top: Double;
begin
if CUTOFF <= 0 then CUTOFF := SRL_GAUSS_CUTOFF;
top := Lo;
if Random()*(Hi-Lo) > Mode-Lo then top := Hi;
Result := CUTOFF+1;
while Result >= CUTOFF do
Result := Abs(Sqrt(-2 * Ln(nzRandom())) * Cos(2 * PI * Random()));
Result := Result / CUTOFF * (top-Mode) + Mode;
end;
(*
SRL.SkewedRand
~~~~~~~~~~~~~~~
.. pascal:: function TSRL.SkewedRand(Mode, Lo, Hi: Double): Double; static;
Integer overload.
*)
function TSRL.SkewedRand(Mode, Lo, Hi: Int64; CUTOFF:Single=0): Int64; static; overload;
begin
Result := Round(SRL.SkewedRand(Mode * 1.00, Lo * 1.00, Hi * 1.00, CUTOFF));
end;
(*
SRL.NormalRange
~~~~~~~~~~~~~~~
.. pascal:: function TSRL.NormalRange(Min, Max: Double): Double;
Generates a random float in the given range, weighted towards the mean.
*)
function TSRL.NormalRange(Min, Max: Double; CUTOFF:Single=0): Double; constref; overload;
begin
if CUTOFF <= 0 then CUTOFF := SRL_GAUSS_CUTOFF;
case Random(0,1) of
0: Result := (Max+Min)/2.0 + TruncatedGauss(0, (Max-Min)/2, CUTOFF);
1: Result := (Max+Min)/2.0 - TruncatedGauss(0, (Max-Min)/2, CUTOFF);
end;
end;
(*
SRL.NormalRange
~~~~~~~~~~~~~~~
.. pascal:: function TSRL.NormalRange(min, max: Int64): Int64;
Integer overload.
*)
function TSRL.NormalRange(Min, Max: Int64; CUTOFF:Single=0): Int64; constref; overload;
begin
if CUTOFF <= 0 then CUTOFF := SRL_GAUSS_CUTOFF;
case Random(0,1) of
0: Result := Round((Max+Min)/2.0 + TruncatedGauss(0, (Max-Min)/2, CUTOFF));
1: Result := Round((Max+Min)/2.0 - TruncatedGauss(0, (Max-Min)/2, CUTOFF));
end;
end;
(*
SRL.RandomPoint
~~~~~~~~~~~~~~~
.. pascal:: function TSRL.RandomPoint(Mean: TPoint; MaxRad: Int32): TPoint;
Generates a random TPoint which weights around ``Mean``, with a max distance
from mean defined by ``MaxRad``.
*)
function TSRL.RandomPoint(Mean: TPoint; MaxRad: Int32; CUTOFF:Single=0): TPoint; constref;
begin
Result.X := NormalRange(Mean.x-maxRad, Mean.x+MaxRad, CUTOFF);
Result.Y := NormalRange(Mean.y-maxRad, Mean.y+MaxRad, CUTOFF);
end;
(*
SRL.RandomPoint
~~~~~~~~~~~~~~~
.. pascal:: function TSRL.RandomPoint(Bounds: TBox): TPoint; overload;
Generates a random TPoint in the bounds of the given box ``Bounds``,
the point weights towards the middle of the box.
*)
function TSRL.RandomPoint(Bounds: TBox; CUTOFF:Single=0): TPoint; constref; overload;
begin
Result.X := NormalRange(Bounds.x1, Bounds.x2, CUTOFF);
Result.Y := NormalRange(Bounds.y1, Bounds.y2, CUTOFF);
end;
(*
SRL.RandomPoint
~~~~~~~~~~~~~~~
.. pascal:: function TSRL.RandomPoint(Rect: TRectangle): TPoint; overload;
Generates a random TPoint in the given rectangle ``Rect``,
The point weights towards the middle of the rectangle.
*)
function TSRL.RandomPoint(Rect: TRectangle; CUTOFF:Single = 0): TPoint; constref; overload;
var
a,x,y,x1,y1,x2,y2: Double;
begin
with Rect do
begin
a := ArcTan2(Left.Y-Top.Y, Left.X-Top.X);
X := (Top.X + Right.X + Btm.X + Left.X) / 4;
Y := (Top.Y + Right.Y + Btm.Y + Left.Y) / 4;
x1 := x-Hypot(Left.y-Top.y, Left.x-Top.x) / 2;
y1 := y-Hypot(Left.y-Btm.y, Left.x-Btm.x) / 2;
x2 := x+Hypot(Left.y-Top.y, Left.x-Top.x) / 2;
y2 := y+Hypot(Left.y-Btm.y, Left.x-Btm.x) / 2;
end;
Result.X := Round(SRL.NormalRange(x1+1, x2-1, CUTOFF));
Result.Y := Round(SRL.NormalRange(y1+1, y2-1, CUTOFF));
Result := RotatePoint(Result, a, (X2+X1)/2+Random()-0.5, (Y2+Y1)/2+Random()-0.5);
end;
(*
SRL.RandonPointEx
~~~~~~~~~~~~~~~~~
.. pascal:: function TSRL.RandonPointEx(From: TPoint; B: TBox; Force: Double=0.35): TPoint; constref;
Generates a random point within the bounds of the given box `B`, the point generated is skewed towards towards the `From`-point.
The last parameter `Force` defines how much the generated point is to be skewed towards or away from `From` - Expects value in the range 0..2
* Force = 0: Result weighs heavily towrads the edge closest to `From`
* Force = 1: Result in the middle of box is most common
* Force = 2: Result weighs heavily towrads the edge furthest away from `From`
*)
function TSRL.RandomPointEx(From: TPoint; B: TBox; Force: Double=0.35): TPoint; constref;
var
p,c: TPoint;
x,r: Double;
begin
p := From;
if p.x < B.x1 then p.x := B.x1 else if p.x > B.x2 then p.x := B.x2;
if p.y < B.y1 then p.y := B.y1 else if p.y > B.y2 then p.y := B.y2;
c := Point((B.X2 + B.X1) div 2, (B.Y2 + B.Y1) div 2);
r := Hypot(p.x-c.x, p.y-c.y) * Force;
x := ArcTan2(c.y-p.y, c.x-p.x);
p.x += Round(Cos(x) * r);
p.y += Round(Sin(x) * r);
Result.x := Round(SkewedRand(p.x, B.x1, B.x2, SRL_GAUSS_CUTOFF));
Result.y := Round(SkewedRand(p.y, B.y1, B.y2, SRL_GAUSS_CUTOFF));
end;
(*
SRL.rowp
~~~~~~~~
.. pascal::
function TSRL.rowp(From: TPoint; Rect: TRectangle; Force: Double=-0.9; Smoothness: Double=PI/12): TPoint;
function TSRL.rowp(From: TPoint; Box: TBox; Force: Double=-0.9; Smoothness: Double=PI/12): TPoint;
rowp, short for `Random Olly Weighted Point`. Generates a random point based on
a rough formula that Olly came up with for weighting points towards "From" point.
Final implementation and math done by slacky.
Force ranges from -1 (close to) to 1 (away from), where 0 is mean, but with a bit of a skewiness...
*)
function TSRL.rowp(From: TPoint; Rect: TRectangle; Force: Double=-0.9; Smoothness: Double=PI/12): TPoint; static;
var
p,e: TPoint;
t, dist: Double;
function NearestEdgeTo(P: TPoint): TPoint;
var
best,dist: Double;
x: TPoint;
begin
best := SRL.DistToLineEx(p, Rect.Top, Rect.Left, Result);
if (dist := SRL.DistToLineEx(p, Rect.Left, Rect.Btm, x)) < best then begin Result := x; Best := dist; end;
if (dist := SRL.DistToLineEx(p, Rect.Btm, Rect.Right,x)) < best then begin Result := x; Best := dist; end;
if (dist := SRL.DistToLineEx(p, Rect.Right,Rect.Top, x)) < best then begin Result := x; Best := dist; end;
end;
begin
p := SRL.RandomPoint(rect, SRL_GAUSS_CUTOFF / 1.5);
e := NearestEdgeTo(P);
dist := Hypot(p.X - e.X, p.Y - e.Y);
t := ArcTan2(p.Y-From.Y, p.X-From.X) + (Random()-0.5) * Smoothness;
Result.X := Round(p.x + Cos(t) * SRL.SkewedRand(dist * force, 0, dist));
Result.Y := Round(p.y + Sin(t) * SRL.SkewedRand(dist * force, 0, dist));
end;
function TSRL.rowp(From: TPoint; Box: TBox; Force: Double=-0.9; Smoothness: Double=PI/12): TPoint; static; overload;
begin
Result := TSRL.rowp(From, Box.ToRectangle(), Force, Smoothness);
end;
(*
SRL.Dice
~~~~~~~~
.. pascal:: function TSRL.Dice(ChancePercent: Double): Boolean; static;
Generates a random number and returns ``True`` whenever the value generated is within the chance (in percentage).
Example
-------
if SRL.Dice(10.5) then
WriteLn('This has a 10.5 percent chance of writing')
else
WriteLn('No dice');
*)
function TSRL.Dice(ChancePercent: Double): Boolean; static;
begin
Result := System.Random() < ChancePercent / 100;
end;
function TSRL.RandomBoxNearby(P: TPoint; Boxes: TBoxArray; CUTOFF: Single=0): Int32; static;
var
Points: TPointArray;
I: Int32;
begin
if CUTOFF <= 0 then CUTOFF := SRL_GAUSS_CUTOFF;
SetLength(Points, Length(Boxes));
for I := 0 to High(Points) do
Points[I] := SRL.RandomPoint(Boxes[I]);
P := Points.Sorted(P)[SRL.TruncatedGauss(0, High(Boxes), CUTOFF)];
for I := 0 to High(Boxes) do
if P.InBox(Boxes[I]) then
Exit(I);
end;
(*
Wait
~~~~
.. pascal:: procedure Wait(Min, Max: Double; Weight: EWaitDir=wdMean); overload;
Waits ... Weighted towards the mean of `Min` and `Max`
*)
procedure Wait(Min, Max:Double; Weight:EWaitDir=wdMean); overload;
begin
case Weight of
wdLeft: System.Wait(Round(SRL.TruncatedGauss(Min, Max)));
wdMean: System.Wait(Round(SRL.NormalRange(Min, Max)));
wdRight: System.Wait(Round(SRL.TruncatedGauss(Max, Min)));
end;
end;
(*
WaitEx
~~~~~~
.. pascal:: procedure WaitEx(Mean, Dev: Double);
Waits ... Regular gauss random
*)
procedure WaitEx(Mean, Dev: Double);
begin
Wait(Abs(Trunc(SRL.GaussRand(Mean, Dev))));
end;