-
-
Notifications
You must be signed in to change notification settings - Fork 231
/
RandomE.cs
708 lines (628 loc) · 24.3 KB
/
RandomE.cs
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
using System;
using UnityEngine;
using System.Collections.Generic;
using System.Text;
using URandom = UnityEngine.Random;
using MRandom = Unity.Mathematics.Random;
namespace ProceduralToolkit
{
/// <summary>
/// Class for generating random data. Contains extensions for arrays and other collections.
/// </summary>
public static class RandomE
{
/// <summary>
/// Returns a random 2D rotation
/// </summary>
public static float rotation2 => URandom.Range(0, 360f);
/// <summary>
/// Returns a random rotation around X axis
/// </summary>
public static Quaternion xRotation => Quaternion.Euler(rotation2, 0, 0);
/// <summary>
/// Returns a random rotation around Y axis
/// </summary>
public static Quaternion yRotation => Quaternion.Euler(0, rotation2, 0);
/// <summary>
/// Returns a random rotation around Z axis
/// </summary>
public static Quaternion zRotation => Quaternion.Euler(0, 0, rotation2);
#region Geometry
/// <summary>
/// Returns a random point on a circle with radius 1
/// </summary>
public static Vector2 onUnitCircle2 => Geometry.PointOnCircle2(1, rotation2);
/// <summary>
/// Returns a random point inside a circle with radius 1
/// </summary>
public static Vector3 insideUnitCircle3XY => Geometry.PointOnCircle3XY(URandom.value, rotation2);
/// <summary>
/// Returns a random point inside a circle with radius 1
/// </summary>
public static Vector3 insideUnitCircle3XZ => Geometry.PointOnCircle3XZ(URandom.value, rotation2);
/// <summary>
/// Returns a random point inside a circle with radius 1
/// </summary>
public static Vector3 insideUnitCircle3YZ => Geometry.PointOnCircle3YZ(URandom.value, rotation2);
/// <summary>
/// Returns a random point on a circle with radius 1
/// </summary>
public static Vector3 onUnitCircle3XY => Geometry.PointOnCircle3XY(1, rotation2);
/// <summary>
/// Returns a random point on a circle with radius 1
/// </summary>
public static Vector3 onUnitCircle3XZ => Geometry.PointOnCircle3XZ(1, rotation2);
/// <summary>
/// Returns a random point on a circle with radius 1
/// </summary>
public static Vector3 onUnitCircle3YZ => Geometry.PointOnCircle3YZ(1, rotation2);
/// <summary>
/// Returns a random point inside a unit square
/// </summary>
public static Vector2 insideUnitSquare => Range(new Vector2(-0.5f, -0.5f), new Vector2(0.5f, 0.5f));
/// <summary>
/// Returns a random point on the perimeter of a unit square
/// </summary>
public static Vector2 onUnitSquare => PointOnRect(new Rect(-0.5f, -0.5f, 1, 1));
/// <summary>
/// Returns a random point inside a unit cube
/// </summary>
public static Vector3 insideUnitCube => Range(new Vector3(-0.5f, -0.5f, -0.5f), new Vector3(0.5f, 0.5f, 0.5f));
/// <summary>
/// Returns a random point on a segment
/// </summary>
public static Vector2 PointOnSegment2(Segment2 segment)
{
return PointOnSegment2(segment.a, segment.b);
}
/// <summary>
/// Returns a random point on a segment
/// </summary>
public static Vector2 PointOnSegment2(Vector2 segmentA, Vector2 segmentB)
{
return Geometry.PointOnSegment2(segmentA, segmentB, URandom.value);
}
/// <summary>
/// Returns a random point on a segment
/// </summary>
public static Vector3 PointOnSegment3(Segment3 segment)
{
return PointOnSegment3(segment.a, segment.b);
}
/// <summary>
/// Returns a random point on a segment
/// </summary>
public static Vector3 PointOnSegment3(Vector3 segmentA, Vector3 segmentB)
{
return Geometry.PointOnSegment3(segmentA, segmentB, URandom.value);
}
/// <summary>
/// Returns a random point on a circle
/// </summary>
public static Vector2 PointOnCircle2(Circle2 circle)
{
return PointOnCircle2(circle.center, circle.radius);
}
/// <summary>
/// Returns a random point on a circle
/// </summary>
public static Vector2 PointOnCircle2(Vector2 center, float radius)
{
return Geometry.PointOnCircle2(center, radius, rotation2);
}
/// <summary>
/// Returns a random point inside a circle
/// </summary>
public static Vector2 PointInCircle2(Circle2 circle)
{
return PointInCircle2(circle.center, circle.radius);
}
/// <summary>
/// Returns a random point inside a circle
/// </summary>
public static Vector2 PointInCircle2(Vector2 center, float radius)
{
return center + URandom.insideUnitCircle*radius;
}
/// <summary>
/// Returns a random point on a sphere
/// </summary>
public static Vector3 PointOnSphere(Sphere sphere)
{
return PointOnSphere(sphere.center, sphere.radius);
}
/// <summary>
/// Returns a random point on a sphere
/// </summary>
public static Vector3 PointOnSphere(Vector3 center, float radius)
{
return center + URandom.onUnitSphere*radius;
}
/// <summary>
/// Returns a random point inside a sphere
/// </summary>
public static Vector3 PointInSphere(Sphere sphere)
{
return PointInSphere(sphere.center, sphere.radius);
}
/// <summary>
/// Returns a random point inside a sphere
/// </summary>
public static Vector3 PointInSphere(Vector3 center, float radius)
{
return center + URandom.insideUnitSphere*radius;
}
/// <summary>
/// Returns a random point inside a <paramref name="rect"/>
/// </summary>
public static Vector2 PointInRect(Rect rect)
{
return Range(rect.min, rect.max);
}
/// <summary>
/// Returns a random point on the perimeter of a <paramref name="rect"/>
/// </summary>
public static Vector2 PointOnRect(Rect rect)
{
float perimeter = 2*rect.width + 2*rect.height;
float value = URandom.value*perimeter;
if (value < rect.width)
{
return rect.min + new Vector2(value, 0);
}
value -= rect.width;
if (value < rect.height)
{
return rect.min + new Vector2(rect.width, value);
}
value -= rect.height;
if (value < rect.width)
{
return rect.min + new Vector2(value, rect.height);
}
return rect.min + new Vector2(0, value - rect.width);
}
/// <summary>
/// Returns a random point inside <paramref name="bounds"/>
/// </summary>
public static Vector3 PointInBounds(Bounds bounds)
{
return Range(bounds.min, bounds.max);
}
#endregion Geometry
#region Colors
/// <summary>
/// Returns a random color between black [inclusive] and white [inclusive]
/// </summary>
public static Color color => new Color(URandom.value, URandom.value, URandom.value);
/// <summary>
/// Returns a color with a random hue and a maximum saturation and value in HSV model
/// </summary>
public static ColorHSV colorHSV => new ColorHSV(URandom.value, 1, 1);
/// <summary>
/// Returns a gradient between two random colors
/// </summary>
public static Gradient gradient => ColorE.Gradient(color, color);
/// <summary>
/// Returns a gradient between two random HSV colors
/// </summary>
public static Gradient gradientHSV => ColorE.Gradient(colorHSV, colorHSV);
/// <summary>
/// Returns a color with a random hue and a given <paramref name="saturation"/> and <paramref name="value"/>
/// </summary>
public static ColorHSV ColorHue(float saturation, float value, float alpha = 1)
{
return new ColorHSV(URandom.value, saturation, value, alpha);
}
/// <summary>
/// Returns a color with a random saturation and given <paramref name="hue"/> and <paramref name="value"/>
/// </summary>
public static ColorHSV ColorSaturation(float hue, float value, float alpha = 1)
{
return new ColorHSV(hue, URandom.value, value, alpha);
}
/// <summary>
/// Returns a color with a random value and given <paramref name="hue"/> and <paramref name="saturation"/>
/// </summary>
public static ColorHSV ColorValue(float hue, float saturation, float alpha = 1)
{
return new ColorHSV(hue, saturation, URandom.value, alpha);
}
/// <summary>
/// Returns an analogous palette based on a color with a random hue
/// </summary>
public static List<ColorHSV> AnalogousPalette(float saturation = 1, float value = 1, float alpha = 1, int count = 2,
bool withComplementary = false)
{
return ColorHue(saturation, value, alpha).GetAnalogousPalette(count, withComplementary);
}
/// <summary>
/// Returns a triadic palette based on a color with a random hue
/// </summary>
public static List<ColorHSV> TriadicPalette(float saturation = 1, float value = 1, float alpha = 1, bool withComplementary = false)
{
return ColorHue(saturation, value, alpha).GetTriadicPalette(withComplementary);
}
/// <summary>
/// Returns a tetradic palette based on a color with a random hue
/// </summary>
public static List<ColorHSV> TetradicPalette(float saturation = 1, float value = 1, float alpha = 1)
{
return ColorHue(saturation, value, alpha).GetTetradicPalette();
}
#endregion Colors
#region Strings
/// <summary>
/// Returns a random alphanumeric 8-character string
/// </summary>
public static string string8 => PTUtils.Alphanumerics.GetRandom(8);
/// <summary>
/// Returns a random alphanumeric 16-character string
/// </summary>
public static string string16 => PTUtils.Alphanumerics.GetRandom(16);
/// <summary>
/// Returns a random lowercase letter
/// </summary>
public static char lowercaseLetter => PTUtils.LowercaseLetters.GetRandom();
/// <summary>
/// Returns a random uppercase letter
/// </summary>
public static char uppercaseLetter => PTUtils.UppercaseLetters.GetRandom();
#endregion Strings
/// <summary>
/// Returns a random element
/// </summary>
public static T GetRandom<T>(this IReadOnlyList<T> list)
{
if (list == null)
{
throw new ArgumentNullException(nameof(list));
}
if (list.Count == 0)
{
throw new ArgumentException("Empty list");
}
return list[URandom.Range(0, list.Count)];
}
/// <summary>
/// Returns a random element
/// </summary>
public static T GetRandom<T>(this IReadOnlyList<T> list, ref MRandom random)
{
if (list == null)
{
throw new ArgumentNullException(nameof(list));
}
if (list.Count == 0)
{
throw new ArgumentException("Empty list");
}
return list[random.NextInt(0, list.Count)];
}
/// <summary>
/// Returns a random element
/// </summary>
public static T GetRandom<T>(T item1, T item2, params T[] items)
{
if (items == null)
{
throw new ArgumentNullException(nameof(items));
}
int index = URandom.Range(0, items.Length + 2);
if (index == 0)
{
return item1;
}
if (index == 1)
{
return item2;
}
return items[index - 2];
}
/// <summary>
/// Returns a random element with the chances of rolling based on <paramref name="weights"/>
/// </summary>
/// <param name="weights">Positive floats representing weights. Negative values may lead to unpredictable behaviour.</param>
public static T GetRandom<T>(this IReadOnlyList<T> list, IReadOnlyList<float> weights)
{
if (list == null)
{
throw new ArgumentNullException(nameof(list));
}
if (list.Count == 0)
{
throw new ArgumentException("Empty list");
}
if (weights == null)
{
throw new ArgumentNullException(nameof(weights));
}
if (weights.Count == 0)
{
throw new ArgumentException("Empty weights");
}
if (list.Count != weights.Count)
{
throw new ArgumentException("Array sizes must be equal");
}
if (list.Count == 1)
{
return list[0];
}
var cumulative = new List<float>(weights);
for (int i = 1; i < cumulative.Count; i++)
{
cumulative[i] += cumulative[i - 1];
}
float randomValue = URandom.Range(0, cumulative[cumulative.Count - 1]);
int index = cumulative.FindIndex(a => a >= randomValue);
if (index == -1)
{
throw new ArgumentException("Weights must be positive");
}
return list[index];
}
/// <summary>
/// Returns a random element with the chances of rolling based on <paramref name="weights"/>
/// </summary>
/// <param name="weights">Positive floats representing weights. Negative values may lead to unpredictable behaviour.</param>
public static T GetRandom<T>(this IReadOnlyList<T> list, IReadOnlyList<float> weights, ref MRandom random)
{
if (list == null)
{
throw new ArgumentNullException(nameof(list));
}
if (list.Count == 0)
{
throw new ArgumentException("Empty list");
}
if (weights == null)
{
throw new ArgumentNullException(nameof(weights));
}
if (weights.Count == 0)
{
throw new ArgumentException("Empty weights");
}
if (list.Count != weights.Count)
{
throw new ArgumentException("Array sizes must be equal");
}
if (list.Count == 1)
{
return list[0];
}
var cumulative = new List<float>(weights);
for (int i = 1; i < cumulative.Count; i++)
{
cumulative[i] += cumulative[i - 1];
}
float randomValue = random.NextFloat(0, cumulative[cumulative.Count - 1]);
int index = cumulative.FindIndex(a => a >= randomValue);
if (index == -1)
{
throw new ArgumentException("Weights must be positive");
}
return list[index];
}
/// <summary>
/// Returns a random value from the dictionary
/// </summary>
public static TValue GetRandom<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
{
if (dictionary == null)
{
throw new ArgumentNullException(nameof(dictionary));
}
var keys = dictionary.Keys;
if (keys.Count == 0)
{
throw new ArgumentException("Empty dictionary");
}
return dictionary[new List<TKey>(keys).GetRandom()];
}
/// <summary>
/// Returns a random value from the dictionary
/// </summary>
public static TValue GetRandom<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, ref MRandom random)
{
if (dictionary == null)
{
throw new ArgumentNullException(nameof(dictionary));
}
var keys = dictionary.Keys;
if (keys.Count == 0)
{
throw new ArgumentException("Empty dictionary");
}
return dictionary[new List<TKey>(keys).GetRandom(ref random)];
}
/// <summary>
/// Returns a random character from the string
/// </summary>
public static char GetRandom(this string chars)
{
if (string.IsNullOrEmpty(chars))
{
throw new ArgumentException("Empty string");
}
return chars[URandom.Range(0, chars.Length)];
}
/// <summary>
/// Returns a string consisting of random characters from that string
/// </summary>
public static string GetRandom(this string chars, int length)
{
if (string.IsNullOrEmpty(chars))
{
throw new ArgumentException("Empty string");
}
var randomString = new StringBuilder(length);
for (int i = 0; i < length; i++)
{
randomString.Append(chars[URandom.Range(0, chars.Length)]);
}
return randomString.ToString();
}
/// <summary>
/// Returns a random element and removes it from the list
/// </summary>
public static T PopRandom<T>(this List<T> list)
{
if (list == null)
{
throw new ArgumentNullException(nameof(list));
}
if (list.Count == 0)
{
throw new ArgumentException("Empty list");
}
int index = URandom.Range(0, list.Count);
T item = list[index];
list.RemoveAt(index);
return item;
}
/// <summary>
/// Returns a random element and removes it from the list
/// </summary>
public static T PopRandom<T>(this List<T> list, ref MRandom random)
{
if (list == null)
{
throw new ArgumentNullException(nameof(list));
}
if (list.Count == 0)
{
throw new ArgumentException("Empty list");
}
int index = random.NextInt(0, list.Count);
T item = list[index];
list.RemoveAt(index);
return item;
}
/// <summary>
/// Fisher–Yates shuffle
/// </summary>
/// <remarks>
/// https://en.wikipedia.org/wiki/Fisher–Yates_shuffle
/// </remarks>
public static void Shuffle<T>(this IList<T> list)
{
if (list == null)
{
throw new ArgumentNullException(nameof(list));
}
for (int i = 0; i < list.Count; i++)
{
int j = URandom.Range(i, list.Count);
T tmp = list[j];
list[j] = list[i];
list[i] = tmp;
}
}
/// <summary>
/// Fisher–Yates shuffle
/// </summary>
/// <remarks>
/// https://en.wikipedia.org/wiki/Fisher–Yates_shuffle
/// </remarks>
public static void Shuffle<T>(this IList<T> list, ref MRandom random)
{
if (list == null)
{
throw new ArgumentNullException(nameof(list));
}
for (int i = 0; i < list.Count; i++)
{
int j = random.NextInt(i, list.Count);
T tmp = list[j];
list[j] = list[i];
list[i] = tmp;
}
}
/// <summary>
/// Returns true with <paramref name="percent"/> probability
/// </summary>
/// <param name="percent">between 0.0 [inclusive] and 1.0 [inclusive]</param>
public static bool Chance(float percent)
{
if (percent == 0) return false;
if (percent == 1) return true;
return URandom.value < percent;
}
#region Range
/// <summary>
/// Returns a random vector between <paramref name="min"/> [inclusive] and <paramref name="max"/> [inclusive]
/// </summary>
public static Vector2 Range(Vector2 min, Vector2 max)
{
return new Vector2(URandom.Range(min.x, max.x), URandom.Range(min.y, max.y));
}
/// <summary>
/// Returns a random vector between <paramref name="min"/> [inclusive] and <paramref name="max"/> [inclusive]
/// </summary>
public static Vector3 Range(Vector3 min, Vector3 max)
{
return new Vector3(URandom.Range(min.x, max.x), URandom.Range(min.y, max.y), URandom.Range(min.z, max.z));
}
/// <summary>
/// Returns a random vector between <paramref name="min"/> [inclusive] and <paramref name="max"/> [inclusive]
/// </summary>
public static Vector4 Range(Vector4 min, Vector4 max)
{
return new Vector4(URandom.Range(min.x, max.x), URandom.Range(min.y, max.y), URandom.Range(min.z, max.z), URandom.Range(min.w, max.w));
}
/// <summary>
/// Returns a random vector between <paramref name="min"/> [inclusive] and <paramref name="max"/> [exclusive]
/// </summary>
public static Vector2Int Range(Vector2Int min, Vector2Int max)
{
return new Vector2Int(URandom.Range(min.x, max.x), URandom.Range(min.y, max.y));
}
/// <summary>
/// Returns a random vector between <paramref name="min"/> [inclusive] and <paramref name="max"/> [exclusive]
/// </summary>
public static Vector3Int Range(Vector3Int min, Vector3Int max)
{
return new Vector3Int(URandom.Range(min.x, max.x), URandom.Range(min.y, max.y), URandom.Range(min.z, max.z));
}
/// <summary>
/// Returns a random float number between and <paramref name="min"/> [inclusive] and <paramref name="max"/> [inclusive].
/// Ensures that there will be only specified amount of variants.
/// </summary>
public static float Range(float min, float max, int variants)
{
if (variants < 2)
{
throw new ArgumentException("Variants must be greater than one");
}
return Mathf.Lerp(min, max, URandom.Range(0, variants)/(variants - 1f));
}
/// <summary>
/// Returns a random vector between and <paramref name="min"/> [inclusive] and <paramref name="max"/> [inclusive].
/// Ensures that there will be only specified amount of variants.
/// </summary>
public static Vector2 Range(Vector2 min, Vector2 max, int variants)
{
return new Vector2(Range(min.x, max.x, variants), Range(min.y, max.y, variants));
}
/// <summary>
/// Returns a random vector between and <paramref name="min"/> [inclusive] and <paramref name="max"/> [inclusive].
/// Ensures that there will be only specified amount of variants.
/// </summary>
public static Vector3 Range(Vector3 min, Vector3 max, int variants)
{
return new Vector3(Range(min.x, max.x, variants), Range(min.y, max.y, variants), Range(min.z, max.z, variants));
}
/// <summary>
/// Returns a random vector between and <paramref name="min"/> [inclusive] and <paramref name="max"/> [inclusive].
/// Ensures that there will be only specified amount of variants.
/// </summary>
public static Vector4 Range(Vector4 min, Vector4 max, int variants)
{
return new Vector4(Range(min.x, max.x, variants), Range(min.y, max.y, variants), Range(min.z, max.z, variants),
Range(min.w, max.w, variants));
}
#endregion Range
}
}