This repository was archived by the owner on May 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathRandomExtensions.cs
554 lines (526 loc) · 21.4 KB
/
RandomExtensions.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
using RCNet.RandomValue;
using System;
using System.Collections.Generic;
namespace RCNet.Extensions
{
/// <summary>
/// Implements useful extensions of the Random class.
/// </summary>
public static class RandomExtensions
{
//Constants
private const double PI2 = 2d * Math.PI;
private static readonly double Log4 = Math.Log(4d);
private static readonly double GammaAlgConst = 1d + Math.Log(4.5d);
/// <summary>
/// Randomly shuffles the elements within an array.
/// </summary>
/// <remarks>
/// Follows the Uniform distribution.
/// </remarks>
/// <param name="array">The array to be shuffled.</param>
/// <param name="rand"></param>
public static void Shuffle<T>(this Random rand, T[] array)
{
int n = array.Length;
while (n > 1)
{
int k = rand.Next(n--);
T temp = array[n];
array[n] = array[k];
array[k] = temp;
}
return;
}
/// <summary>
/// Randomly shuffles the elements within a list.
/// </summary>
/// <remarks>
/// Follows the Uniform distribution.
/// </remarks>
/// <param name="list">A list to be shuffled.</param>
/// <param name="rand"></param>
public static void Shuffle<T>(this Random rand, List<T> list)
{
int n = list.Count;
while (n > 1)
{
int k = rand.Next(n--);
T temp = list[n];
list[n] = list[k];
list[k] = temp;
}
return;
}
/// <summary>
/// Returns the random sign (values 1 or -1).
/// </summary>
/// <remarks>
/// Follows the Uniform distribution.
/// </remarks>
/// <param name="rand"></param>
public static double NextSign(this Random rand)
{
return rand.NextDouble() >= 0.5 ? 1d : -1d;
}
/// <inheritdoc cref="Random.NextDouble"/>
/// <remarks>
/// Follows the Uniform distribution.
/// </remarks>
public static double NextUniformDouble(this Random rand)
{
return rand.NextDouble();
}
/// <summary>
/// Returns a random double within the specified range.
/// </summary>
/// <remarks>
/// Follows the Uniform distribution.
/// </remarks>
/// <param name="min">The min value (inclusive).</param>
/// <param name="max">The max value (exclusive).</param>
/// <param name="rand"></param>
public static double NextRangedUniformDouble(this Random rand, double min = -1, double max = 1)
{
//Check for randomness suppression
if (min == max)
{
return min;
}
//Arguments validations
if (min > max)
{
throw new ArgumentException($"Min is greater than max", "min");
}
//Computation
return rand.NextUniformDouble() * (max - min) + min;
}
/// <summary>
/// Fills an array with random double values within the specified range.
/// </summary>
/// <remarks>
/// <para>
/// Follows the Uniform distribution.
/// </para>
/// </remarks>
/// <param name="array">An array to be filled.</param>
/// <param name="min">The min value (inclusive).</param>
/// <param name="max">The max value (exclusive).</param>
/// <param name="randomSign">Specifies whether to randomize sign.</param>
/// <param name="rand"></param>
public static void FillUniform(this Random rand, double[] array, double min, double max, bool randomSign)
{
for (int i = 0; i < array.Length; i++)
{
array[i] = rand.NextRangedUniformDouble(min, max) * (randomSign ? rand.NextSign() : 1d);
}
return;
}
/// <summary>
/// Returns a random double value.
/// </summary>
/// <remarks>
/// Follows the Gaussian distribution.
/// </remarks>
/// <param name="mean">Required mean.</param>
/// <param name="stdDev">Required standard deviation.</param>
/// <param name="rand"></param>
public static double NextGaussianDouble(this Random rand, double mean = 0, double stdDev = 1)
{
//Uniform (0,1> random doubles
double u1 = 1.0 - rand.NextDouble();
double u2 = 1.0 - rand.NextDouble();
//Computation
return mean + stdDev * Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(PI2 * u2);
}
/// <summary>
/// Returns a random double value.
/// </summary>
/// <remarks>
/// Follows the Gaussian distribution.
/// </remarks>
/// <param name="distrCfg">Configuration of the Gaussian distribution.</param>
/// <param name="rand"></param>
public static double NextGaussianDouble(this Random rand, GaussianDistrSettings distrCfg)
{
return NextGaussianDouble(rand, distrCfg.Mean, distrCfg.StdDev);
}
/// <summary>
/// Returns a random double value.
/// </summary>
/// <remarks>
/// Follows the Gaussian distribution.
/// </remarks>
/// <param name="distrCfg">Configuration of the unsigned Gaussian distribution.</param>
/// <param name="rand"></param>
public static double NextGaussianDouble(this Random rand, UGaussianDistrSettings distrCfg)
{
return NextGaussianDouble(rand, distrCfg.Mean, distrCfg.StdDev);
}
/// <summary>
/// Returns a random double value within the specified range.
/// </summary>
/// <remarks>
/// <para>
/// Follows the Gaussian distribution.
/// </para>
/// <para>
/// Warning: due to application of the filtering loop to get values belonging into the specified range, this function can lead to a bad performance. The performance strongly depends on specified parameters.
/// </para>
/// </remarks>
/// <param name="mean">Required mean.</param>
/// <param name="stdDev">Required standard deviation.</param>
/// <param name="min">The min value (inclusive).</param>
/// <param name="max">The max value (inclusive).</param>
/// <param name="rand"></param>
public static double NextRangedGaussianDouble(this Random rand, double mean, double stdDev, double min, double max)
{
//Check the randomness suppression
if (min == max)
{
return min;
}
//Validations
if (min > max)
{
throw new ArgumentException($"Min is greater than max.", "min and max");
}
//Filtering loop
double result;
do
{
result = rand.NextGaussianDouble(mean, stdDev);
} while (result < min || result > max);
return result;
}
/// <summary>
/// Returns a random double value.
/// </summary>
/// <remarks>
/// Follows the Exponential distribution.
/// </remarks>
/// <param name="mean">Required mean.</param>
/// <param name="rand"></param>
public static double NextExponentialDouble(this Random rand, double mean)
{
//Checks
if (mean == 0)
{
throw new ArgumentException("Mean parameter equals to 0.", "mean");
}
//Lambda
double lambda = 1d / mean;
//Computation
return -Math.Log(1d - rand.NextDouble()) / lambda;
}
/// <summary>
/// Returns a random double value.
/// </summary>
/// <remarks>
/// Follows the Gaussian distribution.
/// </remarks>
/// <param name="distrCfg">Configuration of the Exponential distribution.</param>
/// <param name="rand"></param>
public static double NextExponentialDouble(this Random rand, ExponentialDistrSettings distrCfg)
{
return NextExponentialDouble(rand, distrCfg.Mean);
}
/// <summary>
/// Returns a random double value.
/// </summary>
/// <remarks>
/// Follows the Gaussian distribution.
/// </remarks>
/// <param name="distrCfg">Configuration of the unsigned Exponential distribution.</param>
/// <param name="rand"></param>
public static double NextExponentialDouble(this Random rand, UExponentialDistrSettings distrCfg)
{
return NextExponentialDouble(rand, distrCfg.Mean);
}
/// <summary>
/// Returns a random double value within the specified range.
/// </summary>
/// <remarks>
/// <para>
/// Follows the Exponential distribution.
/// </para>
/// <para>
/// Warning: due to application of the filtering loop to get values belonging into the specified range, this function can lead to a bad performance. The performance strongly depends on specified parameters.
/// </para>
/// </remarks>
/// <param name="mean">Required mean.</param>
/// <param name="min">The min value (inclusive).</param>
/// <param name="max">The max value (inclusive).</param>
/// <param name="rand"></param>
public static double NextRangedExponentialDouble(this Random rand, double mean, double min, double max)
{
//Check the randomness suppression
if (min == max)
{
return min;
}
//Validations
if (min > max)
{
throw new ArgumentException($"Min is greater than max.", "min and max");
}
//Filtering loop
double result;
do
{
result = rand.NextExponentialDouble(mean);
} while (result < min || result > max);
return result;
}
/// <summary>
/// Returns a random double value.
/// </summary>
/// <remarks>
/// <para>
/// Follows the Gaussian distribution.
/// </para>
/// <para>
/// Implementation is converted from Python.
/// Note that Mean tends to alpha/beta and StdDev tends to Sqrt(alpha/(beta*beta)).
/// Generated number is always positive.
/// </para>
/// </remarks>
/// <param name="alpha">The shape parameter (must be greater than 0).</param>
/// <param name="beta">The rate parameter (must be greater than 0).</param>
/// <param name="rand"></param>
public static double NextGammaDouble(this Random rand, double alpha, double beta)
{
//Checks
if (alpha <= 0)
{
throw new ArgumentException("Alpha parameter must be GT 0.", "alpha");
}
if (beta <= 0)
{
throw new ArgumentException("Beta parameter must be GT 0.", "beta");
}
//Computation
if (alpha > 1d)
{
/*
* R.C.H. Cheng, "The generation of Gamma variables with non-integral shape parameters"
* Applied Statistics, (1977), 26, No. 1, p71-74
*/
double ainv = Math.Sqrt(2d * alpha - 1d);
double bbb = alpha - Log4;
double ccc = alpha + ainv;
while (true)
{
double u1 = rand.NextDouble();
if (u1 > 1e-7d && u1 < 0.9999999d)
{
double u2 = 1d - rand.NextDouble();
double v = Math.Log(u1 / (1d - u1)) / ainv;
double x = alpha * Math.Exp(v);
double z = u1 * u1 * u2;
double r = bbb + ccc * v - x;
if (r + GammaAlgConst - 4.5d * z >= 0d || r >= Math.Log(z))
{
return x * beta;
}
}
}
}
else if (alpha == 1d)
{
//Exponential distribution
return -Math.Log(1d - rand.NextDouble()) * beta;
}
else
{
//Algorithm GS of Statistical Computing - Kennedy & Gentle
double x, p, r;
do
{
double b = (Math.E + alpha) / Math.E;
p = rand.NextDouble() * b;
if (p <= 1d)
{
x = Math.Pow(p, (1d / alpha));
}
else
{
x = -Math.Log((b - p) / alpha);
}
r = rand.NextDouble();
} while (!(r <= Math.Exp(-x) || (p > 1d && r <= Math.Pow(x, alpha - 1d))));
return x * beta;
}
}
/// <summary>
/// Returns a random double value.
/// </summary>
/// <remarks>
/// <para>
/// Follows the Gaussian distribution.
/// </para>
/// <para>
/// Implementation is converted from Python.
/// Note that Mean tends to alpha/beta and StdDev tends to Sqrt(alpha/(beta*beta)).
/// Generated number is always positive.
/// </para>
/// </remarks>
/// <param name="distrCfg">Configuration of the Gamma distribution.</param>
/// <param name="rand"></param>
public static double NextGammaDouble(this Random rand, GammaDistrSettings distrCfg)
{
return NextGammaDouble(rand, distrCfg.Alpha, distrCfg.Beta);
}
/// <summary>
/// Returns a random double value within the specified range.
/// </summary>
/// <remarks>
/// <para>
/// Follows the Gamma distribution.
/// </para>
/// <para>
/// Warning: due to application of the filtering loop to get values belonging into the specified range, this function can lead to a bad performance. The performance strongly depends on specified parameters.
/// </para>
/// <para>
/// Implementation is converted from Python.
/// Note that Mean tends to alpha/beta and StdDev tends to Sqrt(alpha/(beta*beta)).
/// Generated number is always positive.
/// </para>
/// </remarks>
/// <param name="alpha">The shape parameter (must be greater than 0).</param>
/// <param name="beta">The rate parameter (must be greater than 0).</param>
/// <param name="min">The min value (inclusive, must be greater than 0).</param>
/// <param name="max">The max value (inclusive, must be greater than 0).</param>
/// <param name="rand"></param>
public static double NextRangedGammaDouble(this Random rand, double alpha, double beta, double min, double max)
{
//Check for randomness suppression
if (min == max)
{
return min;
}
//Arguments validations
if (min < 0)
{
throw new ArgumentException($"Min is less than 0", "min");
}
if (min > max)
{
throw new ArgumentException($"Min is greater than max", "min");
}
//Filterring loop
double result;
do
{
result = rand.NextGammaDouble(alpha, beta);
} while (result < min || result > max);
return result;
}
/// <summary>
/// Returns a random double value according to the specified configuration.
/// </summary>
/// <param name="randomValueCfg">The random value configuration.</param>
/// <param name="rand"></param>
public static double NextDouble(this Random rand, RandomValueSettings randomValueCfg)
{
double value;
switch (randomValueCfg.DistrType)
{
case RandomCommon.DistributionType.Uniform:
value = rand.NextRangedUniformDouble(randomValueCfg.Min, randomValueCfg.Max);
break;
case RandomCommon.DistributionType.Gaussian:
if (randomValueCfg.DistrCfg != null)
{
GaussianDistrSettings gaussianCfg = randomValueCfg.DistrCfg as GaussianDistrSettings;
value = rand.NextRangedGaussianDouble(gaussianCfg.Mean, gaussianCfg.StdDev, randomValueCfg.Min, randomValueCfg.Max);
}
else
{
throw new ArgumentException($"A specific configuration of the Gaussian distribution is missing.", "randomValueCfg");
}
break;
case RandomCommon.DistributionType.Exponential:
if (randomValueCfg.DistrCfg != null)
{
ExponentialDistrSettings exponentialCfg = randomValueCfg.DistrCfg as ExponentialDistrSettings;
value = rand.NextRangedExponentialDouble(exponentialCfg.Mean, randomValueCfg.Min, randomValueCfg.Max);
}
else
{
throw new ArgumentException($"A specific configuration of the Exponential distribution is missing.", "randomValueCfg");
}
break;
case RandomCommon.DistributionType.Gamma:
if (randomValueCfg.DistrCfg != null)
{
GammaDistrSettings gammaCfg = randomValueCfg.DistrCfg as GammaDistrSettings;
value = rand.NextRangedGammaDouble(gammaCfg.Alpha, gammaCfg.Beta, randomValueCfg.Min, randomValueCfg.Max);
}
else
{
throw new ArgumentException($"A specific configuration of the Gamma distribution is missing.", "randomValueCfg");
}
break;
default:
throw new ArgumentException($"Unknown distribution type: {randomValueCfg.DistrType}.", "randomValueCfg");
}
if (randomValueCfg.RandomSign)
{
value *= rand.NextSign();
}
return value;
}
/// <summary>
/// Returns a random double value according to the specified configuration.
/// </summary>
/// <param name="randomValueCfg">The random unsigned value configuration.</param>
/// <param name="rand"></param>
public static double NextDouble(this Random rand, URandomValueSettings randomValueCfg)
{
double value;
switch (randomValueCfg.DistrType)
{
case RandomCommon.DistributionType.Uniform:
value = rand.NextRangedUniformDouble(randomValueCfg.Min, randomValueCfg.Max);
break;
case RandomCommon.DistributionType.Gaussian:
if (randomValueCfg.DistrCfg != null)
{
UGaussianDistrSettings gaussianCfg = randomValueCfg.DistrCfg as UGaussianDistrSettings;
value = rand.NextRangedGaussianDouble(gaussianCfg.Mean, gaussianCfg.StdDev, randomValueCfg.Min, randomValueCfg.Max);
}
else
{
throw new ArgumentException($"A specific configuration of the Gaussian distribution is missing.", "randomValueCfg");
}
break;
case RandomCommon.DistributionType.Exponential:
if (randomValueCfg.DistrCfg != null)
{
UExponentialDistrSettings exponentialCfg = randomValueCfg.DistrCfg as UExponentialDistrSettings;
value = rand.NextRangedExponentialDouble(exponentialCfg.Mean, randomValueCfg.Min, randomValueCfg.Max);
}
else
{
throw new ArgumentException($"A specific configuration of the Exponential distribution is missing.", "randomValueCfg");
}
break;
case RandomCommon.DistributionType.Gamma:
if (randomValueCfg.DistrCfg != null)
{
GammaDistrSettings gammaCfg = randomValueCfg.DistrCfg as GammaDistrSettings;
value = rand.NextRangedGammaDouble(gammaCfg.Alpha, gammaCfg.Beta, randomValueCfg.Min, randomValueCfg.Max);
}
else
{
throw new ArgumentException($"A specific configuration of the Gamma distribution is missing.", "randomValueCfg");
}
break;
default:
throw new ArgumentException($"Unknown distribution type: {randomValueCfg.DistrType}.", "randomValueCfg");
}
return value;
}
}//RandomExtensions
}//Namespace