1+ (*
2+ # Random
3+ Random number generation utilities and weighted random point functions.
4+ *)
5+
16{$DEFINE WL_RANDOM_INCLUDED}
27
38type
9+ (*
10+ ## ERandomDir
11+ Direction bias for random number generation (LEFT, MEAN, RIGHT).
12+ *)
413 ERandomDir = enum(LEFT, MEAN, RIGHT);
14+
15+ (*
16+ ## ERandomDistribution
17+ Distribution type for random generation (RANDOM or GAUSS).
18+ *)
519 ERandomDistribution = enum(RANDOM, GAUSS);
620
21+ (*
22+ ## RandomBoolean
23+ ```pascal
24+ function RandomBoolean(): Boolean;
25+ function RandomBoolean(probability: Double): Boolean; overload;
26+ ```
27+ Returns a random boolean. Optionally weighted by probability (0.0-1.0).
28+ *)
729function RandomBoolean(): Boolean;
830begin
931 Result := Boolean(Random(0,1));
1436 Result := Random() <= probability;
1537end;
1638
39+ (*
40+ ## Sleep (overload)
41+ ```pascal
42+ procedure Sleep(min, max: UInt32; dir: ERandomDir = ERandomDir.LEFT); overload;
43+ ```
44+ Sleeps for a random duration between min and max, biased by direction.
45+ *)
1746procedure Sleep(min, max: UInt32; dir: ERandomDir = ERandomDir.LEFT); overload;
1847begin
1948 case dir of
2352 end;
2453end;
2554
55+ (*
56+ ## TPoint.RandomBetween
57+ ```pascal
58+ function TPoint.RandomBetween(other: TPoint): TPoint;
59+ ```
60+ Returns a random point on the line between Self and other.
61+ *)
2662function TPoint.RandomBetween(other: TPoint): TPoint;
2763var
2864 r: Double;
@@ -32,15 +68,26 @@ begin
3268 Result.Y := Self.Y + Round(r * (other.Y - Self.Y));
3369end;
3470
35-
71+ (*
72+ ## TPointArray.RandomMean
73+ ```pascal
74+ function TPointArray.RandomMean(): TPoint;
75+ ```
76+ Returns a random point from the array, weighted towards the mean.
77+ *)
3678function TPointArray.RandomMean(): TPoint;
3779begin
3880 if Length(Self) = 0 then Exit;
3981 Result := Self.SortFrom(Self.Mean())[RandomLeft(0, High(Self))];
4082end;
4183
42-
43-
84+ (*
85+ ## TCircle.RandomSkewedPoint
86+ ```pascal
87+ function TCircle.RandomSkewedPoint(from: TPoint; force: Double = 0.35): TPoint;
88+ ```
89+ Generates a random point within the circle, skewed towards `from`.
90+ *)
4491function TCircle.RandomSkewedPoint(from: TPoint; force: Double = 0.35): TPoint;
4592var
4693 skewed, rand: TPoint;
@@ -72,7 +119,15 @@ begin
72119 RandCutoff := cutoff;
73120end;
74121
75- //by [slacky](https://slacky.one/)
122+ (*
123+ ## TCircle.RandomWeightedPoint
124+ ```pascal
125+ function TCircle.RandomWeightedPoint(from: TPoint; weight: Single = 10.0; bias: Single = 1): TPoint;
126+ ```
127+ Generates a random point within the circle, weighted towards `from`.
128+
129+ Credits: [slacky](https://slacky.one/)
130+ *)
76131function TCircle.RandomWeightedPoint(from: TPoint; weight: Single = 10.0; bias: Single = 1): TPoint;
77132var
78133 angle, dist, r1, r2, sum, u, t: Single;
@@ -103,7 +158,13 @@ begin
103158 Result.Y := Round(u * randY + t * from.Y);
104159end;
105160
106-
161+ (*
162+ ## TTriangle.RandomSkewedPoint
163+ ```pascal
164+ function TTriangle.RandomSkewedPoint(from: TPoint; force: Double = 0.35): TPoint;
165+ ```
166+ Generates a random point within the triangle, skewed towards `from`.
167+ *)
107168function TTriangle.RandomSkewedPoint(from: TPoint; force: Double = 0.35): TPoint;
108169var
109170 c, skewed: TPoint;
@@ -146,7 +207,7 @@ Parameters:
146207- bias: Introduces a Gaussian spread around the `from` (higher values = wider spread).
147208 - bias = 0 gives a purely weighted distribution towards the `from`.
148209
149- Should expose weight and bias to the user if used as mouse distribution method. weight of 10 might be a little high.
210+ Should expose weight and bias to the user if used as mouse distribution method.
150211
151212Credits: [slacky](https://slacky.one/)
152213*)
@@ -191,8 +252,15 @@ begin
191252 end;
192253end;
193254
255+ (*
256+ ## TPolygon.RandomPoint
257+ ```pascal
258+ function TPolygon.RandomPoint(): TPoint;
259+ ```
260+ Generates a uniformly distributed random point within the polygon.
194261
195- //by torwent
262+ Credits: torwent
263+ *)
196264function TPolygon.RandomPoint(): TPoint;
197265var
198266 tris: TTriangleArray;
@@ -220,7 +288,15 @@ begin
220288 end;
221289end;
222290
223- //by bootie
291+ (*
292+ ## TPolygon.RandomPointCenter
293+ ```pascal
294+ function TPolygon.RandomPointCenter(): TPoint;
295+ ```
296+ Generates a random point within the polygon, weighted towards the center.
297+
298+ Credits: bootie
299+ *)
224300function TPolygon.RandomPointCenter(): TPoint;
225301var
226302 mean: TPoint;
@@ -235,6 +311,13 @@ begin
235311 until Self.Contains(Result);
236312end;
237313
314+ (*
315+ ## TPolygon.RandomSkewedPoint
316+ ```pascal
317+ function TPolygon.RandomSkewedPoint(from: TPoint): TPoint;
318+ ```
319+ Generates a random point within the polygon, skewed towards `from`.
320+ *)
238321function TPolygon.RandomSkewedPoint(from: TPoint): TPoint;
239322begin
240323 Result := Self.RandomWeightedPoint(from);
@@ -285,9 +368,9 @@ function TQuad.RandomSkewedPoint(from: TPoint; force: Double = 0.35): TPoint;
285368Generates a random point within the bounds of the TQuad, the point generated is skewed towards towards the `from`-point.
286369The 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
287370
288- - force = 0: Result weighs heavily towrads the edge closest to `From`
371+ - force = 0: Result weighs heavily towards the edge closest to `From`
289372- force = 1: Result in the middle of box is most common
290- - force = 2: Result weighs heavily towrads the edge furthest away from `From`
373+ - force = 2: Result weighs heavily towards the edge furthest away from `From`
291374*)
292375function TQuad.RandomSkewedPoint(from: TPoint; force: Double = 0.35): TPoint;
293376var
@@ -324,12 +407,12 @@ end;
324407```pascal
325408function TBox.RandomSkewedPoint(from: TPoint; force: Double = 0.35): TPoint;
326409```
327- Generates a random point within the bounds of the TBox, the point generated is skewed towards towards the `from`-point.
410+ Generates a random point within the bounds of the TBox, the point generated is skewed towards the `from`-point.
328411The 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
329412
330- - force = 0: Result weighs heavily towrads the edge closest to `From`
413+ - force = 0: Result weighs heavily towards the edge closest to `From`
331414- force = 1: Result in the middle of box is most common
332- - force = 2: Result weighs heavily towrads the edge furthest away from `From`
415+ - force = 2: Result weighs heavily towards the edge furthest away from `From`
333416*)
334417function TBox.RandomSkewedPoint(from: TPoint; force: Double = 0.35): TPoint;
335418var
@@ -357,7 +440,3 @@ function TBox.RandomWeightedPoint(from: TPoint; weight: Single = 10.0; bias: Sin
357440begin
358441 Result := TPolygon(Self.Corners).RandomWeightedPoint(from, weight, bias);
359442end;
360-
361-
362-
363-
0 commit comments