Repository navigation
Expand file tree
/
Copy pathRandomGenerator.cs
More file actions
888 lines (716 loc) · 30.5 KB
/
Copy pathRandomGenerator.cs
File metadata and controls
888 lines (716 loc) · 30.5 KB
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
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
/*
The MIT License (MIT)
Copyright (c) 2010 Roger Hill
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
namespace RandomNumbers
{
public class RandomGenerator : IRandomGenerator
{
public const string ASCII_ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
private Random _Rand = new();
public RandomGenerator() { }
public RandomGenerator(int seed)
{
_Rand = new Random(seed);
}
#region basic types
public bool Bool()
{
return _Rand.Next(2) == 1;
}
/////////////////////////////////////////////////////////
/// <summary>
/// Returns a random value between [0, 255).
/// </summary>
public byte Byte()
{
return (byte)_Rand.Next(byte.MinValue, byte.MaxValue + 1);
}
/// <summary>
/// Returns a random value between [0, max).
/// </summary>
/// <param name="max">The upper bound of the range, exclusive</param>
public byte Byte(byte max)
{
return Byte(byte.MinValue, max);
}
/// <summary>
/// Returns a random value between [min, max).
/// </summary>
/// <param name="min">The lower bound of the range, inclusive</param>
/// <param name="max">The upper bound of the range, exclusive</param>
public byte Byte(byte min, byte max)
{
return (byte)_Rand.Next(min, max + 1);
}
/////////////////////////////////////////////////////////
public byte[] ByteArray(int count)
{
if (count < 1)
throw new ArgumentException($"Cannot generate {count} bytes, it is less than 1");
byte[] output = new byte[count];
_Rand.NextBytes(output);
return output;
}
/////////////////////////////////////////////////////////
/// <summary>
/// Returns a random value between [0, 65535).
/// </summary>
public char Char()
{
return (char)_Rand.Next(char.MinValue, char.MaxValue + 1);
}
/// <summary>
/// Returns a random value between [0, max).
/// </summary>
/// <param name="max">The upper bound of the range, exclusive</param>
public char Char(char max)
{
return (char)_Rand.Next(char.MinValue, max + 1);
}
/// <summary>
/// Returns a random value between [min, max).
/// </summary>
/// <param name="min">The lower bound of the range, inclusive</param>
/// <param name="max">The upper bound of the range, exclusive</param>
public char Char(char min, char max)
{
return (char)_Rand.Next(min, max + 1);
}
/////////////////////////////////////////////////////////
/// <summary>
/// Returns a random value between [0, 1).
/// </summary>
public double Double()
{
return _Rand.NextDouble();
}
/// <summary>
/// Returns a random value between [0, max).
/// </summary>
/// <param name="max">The upper bound of the range, exclusive</param>
public double Double(double max)
{
if (double.IsNaN(max))
throw new ArgumentOutOfRangeException("max cannot be NaN");
if (double.IsInfinity(max))
throw new ArgumentOutOfRangeException(nameof(max), "max cannot be infinity");
if (max < 0)
throw new ArgumentOutOfRangeException(nameof(max), "max must be > 0");
return _Rand.NextDouble() * max;
}
/// <summary>
/// Returns a random value between min and max parameters.
/// </summary>
/// <param name="min">The lower bound of the range, inclusive</param>
/// <param name="max">The upper bound of the range, exclusive</param>
public double Double(double min, double max)
{
if (double.IsNaN(min))
throw new ArgumentException("min cannot be NaN");
if (double.IsNaN(max))
throw new ArgumentOutOfRangeException("max cannot be NaN");
if (double.IsInfinity(min))
throw new ArgumentOutOfRangeException(nameof(max), "min cannot be infinity");
if (double.IsInfinity(max))
throw new ArgumentOutOfRangeException(nameof(max), "max cannot be infinity");
if (max < min)
throw new ArgumentOutOfRangeException(nameof(max), "max must be >= min.");
return (_Rand.NextDouble() * (max - min)) + min;
}
/////////////////////////////////////////////////////////
/// <summary>
/// Returns a random value between [0, 1).
/// </summary>
public float Float()
{
return _Rand.NextSingle();
}
/// <summary>
/// Returns a random value between [0, max).
/// </summary>
/// <param name="max">The upper bound of the range, exclusive</param>
public float Float(float max)
{
if (float.IsNaN(max))
throw new ArgumentException("max cannot be NaN");
if (float.IsInfinity(max))
throw new ArgumentOutOfRangeException(nameof(max), "max cannot be infinity");
if (max < 0)
throw new ArgumentOutOfRangeException(nameof(max), "max must be >= 0");
return _Rand.NextSingle() * max;
}
/// <summary>
/// Returns a random value between [min,max).
/// </summary>
/// <param name="min">The lower bound of the range, inclusive</param>
/// <param name="max">The upper bound of the range, exclusive</param>
public float Float(float min, float max)
{
if (float.IsNaN(min))
throw new ArgumentException("min cannot be NaN");
if (float.IsNaN(max))
throw new ArgumentException("max cannot be NaN");
if (float.IsInfinity(min))
throw new ArgumentOutOfRangeException(nameof(min), "min cannot be infinity");
if (float.IsInfinity(max))
throw new ArgumentOutOfRangeException(nameof(max), "max cannot be infinity");
if (max < min)
throw new ArgumentOutOfRangeException(nameof(max), "max must be >= min.");
return ((_Rand.NextSingle() * (max - min)) + min);
}
/////////////////////////////////////////////////////////
/// <summary>
/// Returns a random value between [min, int.MaxValue).
/// </summary>
public int Int()
{
return _Rand.Next();
}
/// <summary>
/// Returns a random value between [0, max).
/// </summary>
/// <param name="max">The upper bound of the range, exclusive</param>
public int Int(int max)
{
return _Rand.Next(0, max);
}
/// <summary>
/// Returns a random value between [min, max).
/// </summary>
/// <param name="min">The lower bound of the range, inclusive</param>
/// <param name="max">The upper bound of the range, exclusive</param>
public int Int(int min, int max)
{
if (max < min)
throw new ArgumentOutOfRangeException(nameof(max), "max must be >= min.");
return _Rand.Next(min, max);
}
/////////////////////////////////////////////////////////
/// <summary>
/// Returns a random value between [0, long.MaxValue).
/// </summary>
public long Long()
{
return _Rand.NextInt64();
}
/// <summary>
/// Returns a random value between [0, max).
/// </summary>
/// <param name="max">The upper bound of the range, exclusive</param>
public long Long(long max)
{
return _Rand.NextInt64(max);
}
/// <summary>
/// Returns a random value between [min, max).
/// </summary>
/// <param name="min">The lower bound of the range, inclusive</param>
/// <param name="max">The upper bound of the range, exclusive</param>
public long Long(long min, long max)
{
return _Rand.NextInt64(min, max);
}
/////////////////////////////////////////////////////////
/// <summary>
/// Returns a random value between [0, short.MaxValue).
/// </summary>
public short Short()
{
return (short)_Rand.Next(short.MinValue, short.MaxValue + 1);
}
/// <summary>
/// Returns a random value between [0, max).
/// </summary>
/// <param name="max">The upper bound of the range, exclusive</param>
public short Short(short max)
{
return (short)_Rand.Next(0, max + 1);
}
/// <summary>
/// Returns a random value between [min, max).
/// </summary>
/// <param name="min">The lower bound of the range, inclusive</param>
/// <param name="max">The upper bound of the range, exclusive</param>
public short Short(short min, short max)
{
return (short)_Rand.Next(min, max + 1);
}
/////////////////////////////////////////////////////////
/// <summary>
/// Returns a random value between [0, ulong.MaxValue).
/// </summary>
public ulong ULong()
{
Span<byte> buffer = stackalloc byte[8];
_Rand.NextBytes(buffer);
return BitConverter.ToUInt64(buffer);
}
#endregion
#region math & geometry
/// <summary>
/// Returns a random float value between [0,1].
/// </summary>
public float UnitFloat()
{
Span<byte> buffer = stackalloc byte[4];
_Rand.NextBytes(buffer);
var randVal = BitConverter.ToUInt32(buffer);
return randVal / (float)uint.MaxValue;
}
/// <summary>
/// Returns a random double value between [0,1].
/// </summary>
public double UnitDouble()
{
Span<byte> buffer = stackalloc byte[8];
_Rand.NextBytes(buffer);
var randVal = BitConverter.ToUInt64(buffer);
return randVal / (double)ulong.MaxValue;
}
/// <summary>
/// Returns a value between [0,2pi).
/// </summary>
public float Facing()
{
return (float)(_Rand.NextSingle() * Math.Tau);
}
#endregion
#region strings
/// <summary>
/// Generates a string of N length populated with random unicode characters.
/// </summary>
public string UnicodeString(int length)
{
char[] buffer = new char[length * 2]; // Worst-case: all supplementary characters take 2 chars each
int charPos = 0; // position in char array
int charsAdded = 0; // counts actual number of characters
for (int i = 0; i < length; i++)
{
int codePoint;
// Generate code points in two ranges to skip surrogates efficiently
// 0x0000–0xD7FF and 0xE000–0x10FFFF
if (_Rand.NextDouble() < 0.844) // roughly proportional sizes: 0xD800 / 0x110000
{
codePoint = _Rand.Next(0xD800);
buffer[charPos++] = (char)codePoint;
}
else
{
codePoint = 0xE000 + _Rand.Next(0x10FFFF - 0xE000 + 1);
// Supplementary character → surrogate pair
codePoint -= 0x10000;
buffer[charPos++] = (char)((codePoint >> 10) + 0xD800);
buffer[charPos++] = (char)((codePoint & 0x3FF) + 0xDC00);
}
charsAdded++;
}
return new string(buffer, 0, charPos);
}
public string String(int length)
{
return String(length, ASCII_ALPHABET);
}
public string String(int minLength, int maxLength)
{
int length = Int(minLength, maxLength);
return String(length, ASCII_ALPHABET);
}
public string String(int length, string characterSet)
{
var output = new char[length];
for (int i = 0; i < length; i++)
output[i] = characterSet[Int(characterSet.Length - 1)];
return new string(output);
}
public string Sentence(int sentenceLength)
{
if (sentenceLength < 1)
throw new ArgumentException("Max string length cannot be less than 2");
var output = new StringBuilder(sentenceLength);
int remainingCharacters = sentenceLength;
string buffer;
while (remainingCharacters > 10)
{
buffer = String(1, 7);
output.Append(buffer);
output.Append(' ');
remainingCharacters -= (buffer.Length + 1);
}
output.Append(String(1, remainingCharacters - 1));
output.Append('.');
return output.ToString();
}
public string TextContent(int wordCount, string[] wordList)
{
if (wordCount < 1)
throw new ArgumentException(nameof(wordCount), "Max string length cannot be less than 1");
if (wordList == null || wordList.Length == 0)
throw new ArgumentNullException(nameof(wordList));
var sb = new StringBuilder();
for (int i = 0; i <= wordCount; i++)
{
sb.Append(' ');
sb.Append(wordList[_Rand.Next(wordList.Length - 1)]);
}
sb.Append('.');
return sb.ToString();
}
/// <summary>
/// Generates a completely random RGB color string (ex: "#ff7700")
/// </summary>
public string RGBColorString()
{
byte r = Byte();
byte g = Byte();
byte b = Byte();
return $"#{r:X2}{g:X2}{b:X2}";
}
/// <summary>
/// Generates a completely random RGBA color string (ex: "#ff770077")
/// </summary>
public string RGBAColorString()
{
byte r = Byte();
byte g = Byte();
byte b = Byte();
byte a = Byte();
return $"#{r:X2}{g:X2}{b:X2}{a:X2}";
}
/// <summary>
/// Generates a completely random RGB color string (ex: "#ff7700") that
/// is centered around an input color, varying up to half the input variance.
/// </summary>
public string ColorString(float red, float green, float blue, float variance)
{
if (red < 0 || red > 1f || green < 0 || green > 1f || blue < 0 || blue > 1f)
throw new ArgumentException("Input color values must be between 0 and 1, inclusive");
if (variance < 0 || variance > 1f)
throw new ArgumentException("Variance must be between 0 and 1, inclusive");
float delta = variance / 2;
red = UnitRangeClamp(red + Float(-delta, delta));
green = UnitRangeClamp(green + Float(-delta, delta));
blue = UnitRangeClamp(blue + Float(-delta, delta));
var bRed = (byte)(red * 255);
var bGreen = (byte)(green * 255);
var bBlue = (byte)(blue * 255);
return $"#{bRed:X2}{bGreen:X2}{bBlue:X2}";
}
/// <summary>
/// Clamps a value between [0,1]
/// </summary>
public float UnitRangeClamp(float value)
{
return (value < 0) ? 0 : (value > 1) ? 1 : value;
}
#endregion
#region RandomObjects
/// <summary>
/// Returns a random element from a collection.
/// </summary>
public T CollectionValue<T>(IList<T> collection, bool remove)
{
if (collection == null || collection.Count == 0)
throw new ArgumentException("Collection cannot be null or empty");
int index = Int(collection.Count - 1);
T item = collection[index];
if (remove)
collection.RemoveAt(index);
return item;
}
public V DictionaryValue<K, V>(IDictionary<K, V> dictionary, bool remove)
{
int elementNumber = _Rand.Next(0, dictionary.Count);
if (remove)
{
V value = dictionary.ElementAt(elementNumber).Value;
dictionary.Remove(dictionary.ElementAt(elementNumber).Key);
return value;
}
else
{
return dictionary.ElementAt(elementNumber).Value;
}
}
/// <summary>
/// Returns a random value selected from an enumeration.
/// </summary>
public T EnumValue<T>() where T : struct, IConvertible
{
Array buffer = Enum.GetValues(typeof(T));
return (T)buffer.GetValue(_Rand.Next(buffer.Length));
}
/// <summary>
/// Takes existing class, and randomizes all atomic values
/// </summary>
public T Object<T>() where T : class, new()
{
T output = new();
foreach (var propInfo in typeof(T).GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
{
if (!propInfo.CanWrite)
continue;
Type propType = propInfo.PropertyType;
string propName = propInfo.PropertyType.FullName; // change to .Name? shorter
// in the event that we are looking at a nullable type, we need to look at the underlying type.
if (propType.IsGenericType && propType.GetGenericTypeDefinition() == typeof(Nullable<>))
propName = Nullable.GetUnderlyingType(propType).ToString();
if (propType.IsEnum)
{
// todo: this is a hack. Need to figure out how to get the type of the enum?
}
object outputValue;
switch (propName)
{
case "System.Int32":
outputValue = Int();
break;
case "System.String":
outputValue = String(10);
break;
case "System.Double":
outputValue = Double();
break;
case "System.Float":
outputValue = Float();
break;
case "System.Boolean":
outputValue = Bool();
break;
case "System.DateTime":
outputValue = RandomDateTime();
break;
case "System.Guid":
outputValue = Guid.NewGuid();
break;
case "System.Single":
outputValue = Float();
break;
case "System.Decimal":
outputValue = (decimal)Double();
break;
case "System.Byte":
outputValue = Byte();
break;
case "System.Byte[]":
outputValue = ByteArray(Int(1, 10));
break;
case "System.Char":
outputValue = Char();
break;
case "System.UInt32":
outputValue = (uint)Int();
break;
case "System.Int64":
outputValue = Long();
break;
case "System.UInt64":
outputValue = ULong();
break;
case "System.Object":
outputValue = new object();
break;
case "System.Int16":
outputValue = Short();
break;
case "System.UInt16":
outputValue = (ushort)Short();
break;
default:
throw new Exception($"Column {propName} has an unknown data type: {propType}.");
}
propInfo.SetValue(output, outputValue, null);
}
return output;
}
#endregion
#region Nonuniform distribution
/// <summary>
/// Generates a random value between 0 and the upper bound that is distributed normally.
/// </summary>
/// <param name="upperBound">inclusive highest value</param>
/// <param name="rolls">the number of normally distributed random factors that contribute
/// to the final value. Greater values will push the average towards the mean.</param>
public double NormallyDistributedDouble(double upperBound, int rolls)
{
return NormallyDistributedDouble(0, upperBound, rolls);
}
/// <summary>
/// Generates a random value in the specified range that is distributed normally,
/// using an Irwin-Hall approximation.
/// </summary>
/// <param name="lowerBound">inclusive lowest value</param>
/// <param name="upperBound">inclusive highest value</param>
/// <param name="rolls">the number of normally distributed random factors that contribute
/// to the final value. Greater values will push the average towards the mean.</param>
public double NormallyDistributedDouble(double lowerBound, double upperBound, int rolls)
{
if (rolls < 1)
throw new ArgumentOutOfRangeException(nameof(rolls));
double lowerRollBound = lowerBound / rolls;
double upperRollBound = upperBound / rolls;
double sum = 0.0;
while (rolls > 0)
{
sum += this.Double(lowerRollBound, upperRollBound);
rolls--;
}
return sum;
}
/// <summary>
/// Generates a random value between 0 and the upper bound that is distributed normally.
/// </summary>
/// <param name="upperBound">inclusive highest value</param>
/// <param name="rolls">the number of normally distributed random factors that contribute
/// to the final value. Greater values will push the average towards the mean.</param>
public float NormallyDistributedFloat(float upperBound, int rolls)
{
return NormallyDistributedFloat(0f, upperBound, rolls);
}
/// <summary>
/// Generates a random value in the specified range that is distributed normally.
/// </summary>
/// <param name="lowerBound">inclusive lowest value</param>
/// <param name="upperBound">inclusive highest value</param>
/// <param name="rolls">the number of normally distributed random factors that contribute
/// to the final value. Greater values will push the average towards the mean.</param>
public float NormallyDistributedFloat(float lowerBound, float upperBound, int rolls)
{
return (float)NormallyDistributedDouble(lowerBound, upperBound, rolls);
}
/// <summary>
/// Generates a random value between 0 and the upper bound that is distributed normally.
/// </summary>
/// <param name="upperBound">inclusive highest value</param>
/// <param name="rolls">the number of normally distributed random factors that contribute
/// to the final value. Greater values will push the average towards the mean.</param>
public int NormallyDistributedInt(int upperBound, int rolls)
{
return NormallyDistributedInt(0, upperBound, rolls);
}
/// <summary>
/// Generates a random value in the specified range that is distributed normally.
/// </summary>
/// <param name="lowerBound">inclusive lowest value</param>
/// <param name="upperBound">inclusive highest value</param>
/// <param name="rolls">the number of normally distributed random factors that contribute
/// to the final value. Greater values will push the average towards the mean.</param>
public int NormallyDistributedInt(int lowerBound, int upperBound, int rolls)
{
return (int)NormallyDistributedDouble(lowerBound, upperBound, rolls);
}
/// <summary>
/// Generates a random number in a normal distribution between 0.0 and 1, inclusive.
/// </summary>
public double UniformDistributedDouble(double peak, double scale)
{
// based off of article:
// http://blogs.msdn.com/b/ericlippert/archive/2012/02/21/generating-random-non-uniform-data-in-c.aspx
// turns out the integral of a Uniform Distribution is a stretched Arctan function. Who knew?
//p --> peak + scale * tan(pi * (p - 0.5))
double result = (peak + scale) * Math.Tan(Math.PI * (_Rand.NextDouble() - 0.5));
return result;
}
/// <summary>
/// Generates a true normally distributed (Gaussian) random value using the Box–Muller
/// transform, centered on the specified mean with the given standard deviation.
/// </summary>
/// <param name="mean"></param>
/// <param name="standardDeviation"></param>
/// <returns></returns>
public double GaussianNormalDistribution(double mean, double standardDeviation)
{
double u1 = 1.0 - _Rand.NextDouble();
double u2 = 1.0 - _Rand.NextDouble();
double randStdNormal = Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2.0 * Math.PI * u2);
return mean + standardDeviation * randStdNormal;
}
/// <summary>
/// Generates a random number between 0.0 and 1.0 that is exponentially biased
/// towards 0.0 using a base 10 Logarithm.
/// </summary>
public double ExponentiallyDistributedDouble()
{
// generate a random number between 1 and 10 inclusive
// we use log10, so log10(1) = 0, log10(10) = 1
double random_number = (_Rand.NextDouble() * 9) + 1;
// get log of random value.
double output = Math.Log10(random_number);
// return inverted value.
return 1.0 - output;
}
/// <summary>
/// Generates a random number between 0.0 and 1.0 that is exponentially biased
/// towards 0.0 using a base X Logarithm.
/// </summary>
/// <param name="logBase">base of log to use for computation. Value must be
/// greater than 0. The larger the base, the more probably values close to
/// 0 will be generated.</param>
public double ExponentiallyDistributedDouble(double logBase)
{
if (logBase < double.Epsilon)
throw new ArgumentException("Log base must be greater than double.Epsilon");
// generate a random number between 1 and X inclusive
// we use logX, so logX(1) = 0, logX(X) = 1
double random_number = (_Rand.NextDouble() * (logBase - 1)) + 1;
// get log of random value using specified base.
double output = Math.Log(random_number, logBase);
// return inverted value.
return 1.0 - output;
}
/// <summary>
/// Generates a random number between 0.0 and 1.0 that is exponentially biased
/// towards 0.0 using a base X Logarithm.
/// </summary>
/// <param name="logBase">base of log to use for computation. Value must be
/// greater than 0. The larger the base, the more probably values close to
/// 0 will be generated.</param>
/// <param name="min">The lower bound of the range, inclusive</param>
/// <param name="max">The upper bound of the range, inclusive</param>
public double ExponentiallyDistributedDouble(double logBase, double min, double max)
{
if (min > max)
throw new ArgumentException("Minimum value is greater than maximum value");
return (ExponentiallyDistributedDouble() * (max - min)) + min;
}
#endregion
#region Time
public DateTime RandomTime()
{
int hour = _Rand.Next(0, 23);
int mins = _Rand.Next(0, 59);
int secs = _Rand.Next(0, 59);
var now = DateTime.Now;
return new DateTime(now.Year, now.Month, now.Day, hour, mins, secs);
}
public DateTime RandomDateTime()
{
long ticks = _Rand.NextInt64(DateTime.MinValue.Ticks, DateTime.MaxValue.Ticks);
return new DateTime(ticks, DateTimeKind.Utc);
}
public DateTime RandomDateTime(DateTime min, DateTime max)
{
long ticks;
if (min < max)
ticks = Long(min.Ticks, max.Ticks);
else
ticks = Long(max.Ticks, min.Ticks);
return new DateTime(ticks, DateTimeKind.Local);
}
#endregion
}
}