-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProtoRandom.cs
603 lines (518 loc) · 20.2 KB
/
ProtoRandom.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
using System;
using System.Security.Cryptography;
using System.Collections.Generic;
using System.Numerics;
namespace ProtoRandom
{
public class ProtoRandom
{
/// <summary>
/// Complexity of the selection of the random value.
/// </summary>
public int Complexity { get; set; }
/// <summary>
/// Create a new instance of ProtoRandom.
/// </summary>
/// <param name="complexity">Complexity of selection of the random value.</param>
public ProtoRandom(int complexity = 100)
{
Complexity = complexity;
}
/// <summary>
/// Generate a random byte array of a specific size.
/// </summary>
/// <param name="size">Size of the byte array.</param>
/// <returns>A random byte array.</returns>
public byte[] GetRandomByteArray(int size)
{
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] random = new byte[size];
rng.GetBytes(random);
return random;
}
/// <summary>
/// Generate a random byte array of a specific size.
/// </summary>
/// <param name="size">Size of the byte array.</param>
/// <returns>A random byte array.</returns>
public byte[] GetRandomBytes(int size)
{
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] random = new byte[size];
rng.GetBytes(random);
return random;
}
/// <summary>
/// Generate a random byte array with a size between min and max values.
/// </summary>
/// <param name="min">Minimum size of the byte array.</param>
/// <param name="max">Maximum size of the byte array.</param>
/// <returns>A random byte array.</returns>
public byte[] GetRandomByteArray(int min, int max)
{
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] random = new byte[GetRandomInt32(min, max)];
rng.GetBytes(random);
return random;
}
/// <summary>
/// Generate a random byte array with a size between min and max values.
/// </summary>
/// <param name="min">Minimum size of the byte array.</param>
/// <param name="max">Maximum size of the byte array.</param>
/// <returns>A random byte array.</returns>
public byte[] GetRandomBytes(int min, int max)
{
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] random = new byte[GetRandomInt32(min, max)];
rng.GetBytes(random);
return random;
}
/// <summary>
/// Generate a random integer (Int32).
/// </summary>
/// <returns>A random integer (Int32).</returns>
public int GetRandomInt32()
{
List<int[]> arrays = new List<int[]>();
for (int i = 0; i < Complexity; i++)
{
int[] values = new int[Complexity];
for (int j = 0; j < Complexity; j++)
{
values[j] = GetBasicRandomInt32();
}
arrays.Add(values);
}
return arrays[GetBasicRandomInt32() % Complexity][GetBasicRandomInt32() % Complexity];
}
/// <summary>
/// Generate a random integer (Int32).
/// </summary>
/// <param name="max">Maximum value of the integer.</param>
/// <returns>A random integer (Int32).</returns>
public int GetRandomInt32(int max)
{
return GetRandomInt32() % (max + 1);
}
/// <summary>
/// Generate a random integer (Int32).
/// </summary>
/// <param name="min">Minimum value of the integer.</param>
/// <param name="max">Maximum value of the integer.</param>
/// <returns>A random integer (Int32).</returns>
public int GetRandomInt32(int min, int max)
{
return GetRandomInt32() % (max - min + 1) + min;
}
/// <summary>
/// Generate a random boolean.
/// </summary>
/// <returns>A random boolean.</returns>
public bool GetRandomBoolean()
{
if (GetRandomInt32(1) == 0)
{
return false;
}
return true;
}
/// <summary>
/// Generate a random string with specific length and set of characters.
/// </summary>
/// <param name="chars">Set of characters.</param>
/// <param name="length">Length of the random string.</param>
/// <returns>A random string.</returns>
public string GetRandomString(char[] chars, int length)
{
string value = "";
for (int i = 0; i < length; i++)
{
value += chars[GetRandomInt32(0, chars.Length - 1)];
}
return value;
}
/// <summary>
/// Generate a random string with specific length and set of characters.
/// </summary>
/// <param name="chars">Set of characters.</param>
/// <param name="length">Length of the random string.</param>
/// <returns>A random string.</returns>
public string GetRandomString(string chars, int length)
{
string value = "";
for (int i = 0; i < length; i++)
{
value += chars[GetRandomInt32(0, chars.Length - 1)];
}
return value;
}
/// <summary>
/// Generate a random string with length from min to max value and specific set of characters.
/// </summary>
/// <param name="chars">Set of characters.</param>
/// <param name="min">Minimum length of the string.</param>
/// <param name="max">Maximum length of the string.</param>
/// <returns>A random string.</returns>
public string GetRandomString(char[] chars, int min, int max)
{
return GetRandomString(chars, GetRandomInt32(min, max));
}
/// <summary>
/// Generate a random string with length from min to max value and specific set of characters.
/// </summary>
/// <param name="chars">Set of characters.</param>
/// <param name="min">Minimum length of the string.</param>
/// <param name="max">Maximum length of the string.</param>
/// <returns>A random string.</returns>
public string GetRandomString(string chars, int min, int max)
{
return GetRandomString(chars, GetRandomInt32(min, max));
}
/// <summary>
/// Generate a random unsigned integer (UInt32).
/// </summary>
/// <returns>A random unsigned integer (UInt32).</returns>
public uint GetRandomUInt32()
{
List<uint[]> arrays = new List<uint[]>();
for (int i = 0; i < Complexity; i++)
{
uint[] values = new uint[Complexity];
for (int j = 0; j < Complexity; j++)
{
values[j] = GetBasicRandomUInt32();
}
arrays.Add(values);
}
return arrays[GetBasicRandomInt32() % Complexity][GetBasicRandomInt32() % Complexity];
}
/// <summary>
/// Generate a random unsigned integer (UInt32) with a maximum value.
/// </summary>
/// <param name="max">Maximum value of the uint.</param>
/// <returns>A random unsigned integer (UInt32).</returns>
public uint GetRandomUInt32(uint max)
{
return uint.Parse(Modulo(BigInteger.Parse(GetRandomUInt32().ToString()), BigInteger.Parse((max + 1).ToString())).ToString());
}
/// <summary>
/// Generate a random unsigned integer (UInt32) between a minimum and a maximum value.
/// </summary>
/// <param name="min">Minimum value of the uint.</param>
/// <param name="max">Maximum value of the uint.</param>
/// <returns>A random unsigned integer (UInt32).</returns>
public uint GetRandomUInt32(uint min, uint max)
{
return uint.Parse(Modulo(BigInteger.Parse(GetRandomUInt32().ToString()), BigInteger.Parse((max - min + 1).ToString())).ToString()) + min;
}
/// <summary>
/// Generate a random short (Int16).
/// </summary>
/// <returns>A random short (Int16).</returns>
public short GetRandomInt16()
{
List<short[]> arrays = new List<short[]>();
for (int i = 0; i < Complexity; i++)
{
short[] values = new short[Complexity];
for (int j = 0; j < Complexity; j++)
{
values[j] = GetBasicRandomInt16();
}
arrays.Add(values);
}
return arrays[GetBasicRandomInt32() % Complexity][GetBasicRandomInt32() % Complexity];
}
/// <summary>
/// Generate a random short (Int16) between 0 and a maximum value.
/// </summary>
/// <param name="max">Maximum value of the short.</param>
/// <returns>A random short (Int16).</returns>
public short GetRandomInt16(short max)
{
return short.Parse(Modulo(BigInteger.Parse(GetRandomInt16().ToString()), BigInteger.Parse((max + 1).ToString())).ToString());
}
/// <summary>
/// Generate a random unsigned short (UInt16).
/// </summary>
/// <returns>A random unsigned short (UInt16).</returns>
public ushort GetRandomUInt16()
{
List<ushort[]> arrays = new List<ushort[]>();
for (int i = 0; i < Complexity; i++)
{
ushort[] values = new ushort[Complexity];
for (int j = 0; j < Complexity; j++)
{
values[j] = GetBasicRandomUInt16();
}
arrays.Add(values);
}
return arrays[GetBasicRandomInt32() % Complexity][GetBasicRandomInt32() % Complexity];
}
/// <summary>
/// Generate a random unsigned short (UInt16) between 0 and a maximum value.
/// </summary>
/// <param name="max">Maximum value of the ushort.</param>
/// <returns>A random unsigned short (UInt16).</returns>
public ushort GetRandomUInt16(ushort max)
{
return ushort.Parse(Modulo(BigInteger.Parse(GetRandomInt16().ToString()), BigInteger.Parse((max + 1).ToString())).ToString());
}
/// <summary>
/// Generate a random long (Int64).
/// </summary>
/// <returns></returns>
public long GetRandomInt64()
{
List<long[]> arrays = new List<long[]>();
for (int i = 0; i < Complexity; i++)
{
long[] values = new long[Complexity];
for (int j = 0; j < Complexity; j++)
{
values[j] = GetBasicRandomInt64();
}
arrays.Add(values);
}
return arrays[GetBasicRandomInt32() % Complexity][GetBasicRandomInt32() % Complexity];
}
/// <summary>
/// Generate a random long (Int64) between 0 and a maximum value.
/// </summary>
/// <param name="max">Maximum value of the long.</param>
/// <returns>A random long (Int64).</returns>
public long GetRandomInt64(long max)
{
return GetRandomInt64() % (max + 1);
}
/// <summary>
/// Generate a random long (Int64) between a minimum and a maximum value.
/// </summary>
/// <param name="min">Minimum value of the long.</param>
/// <param name="max">Maximum value of the long.</param>
/// <returns>A random long (Int64).</returns>
public long GetRandomInt64(long min, long max)
{
return GetRandomInt64() % (max - min + 1) + min;
}
/// <summary>
/// Generate a random unsigned long (UInt64).
/// </summary>
/// <returns>A random unsigned long (UInt64).</returns>
public ulong GetRandomUInt64()
{
List<ulong[]> arrays = new List<ulong[]>();
for (int i = 0; i < Complexity; i++)
{
ulong[] values = new ulong[Complexity];
for (int j = 0; j < Complexity; j++)
{
values[j] = GetBasicRandomUInt64();
}
arrays.Add(values);
}
return arrays[GetBasicRandomInt32() % Complexity][GetBasicRandomInt32() % Complexity];
}
/// <summary>
/// Generate a random unsigned long (UInt64) between 0 and a maximum value.
/// </summary>
/// <param name="max">Maximum value of the ulong.</param>
/// <returns>A random unsigned long (UInt64).</returns>
public ulong GetRandomUInt64(ulong max)
{
return ulong.Parse(Modulo(BigInteger.Parse(GetRandomUInt64().ToString()), BigInteger.Parse((max + 1).ToString())).ToString());
}
/// <summary>
/// Generate a random unsigned long (UInt64) between 0 and a maximum value.
/// </summary>
/// <param name="min">Minimum value of the ulong.</param>
/// <param name="max">Maximum value of the ulong.</param>
/// <returns>A random unsigned long (UInt64).</returns>
public ulong GetRandomUInt64(ulong min, ulong max)
{
return ulong.Parse(Modulo(BigInteger.Parse(GetRandomUInt64().ToString()), BigInteger.Parse((max - min + 1).ToString())).ToString()) + min;
}
/// <summary>
/// Generate a random double.
/// </summary>
/// <returns>A random double.</returns>
public double GetRandomDouble()
{
List<double[]> arrays = new List<double[]>();
for (int i = 0; i < Complexity; i++)
{
double[] values = new double[Complexity];
for (int j = 0; j < Complexity; j++)
{
values[j] = GetBasicRandomDouble();
}
arrays.Add(values);
}
return arrays[GetBasicRandomInt32() % Complexity][GetBasicRandomInt32() % Complexity];
}
/// <summary>
/// Generate a random double between 0 and a maximum value.
/// </summary>
/// <param name="max">Maximum value of the double.</param>
/// <returns>A random double.</returns>
public double GetRandomDouble(double max)
{
return Modulo(GetRandomDouble(), max + 1);
}
/// <summary>
/// Generate a random double between a minimum and maximum value.
/// </summary>
/// <param name="min">Minimum value of the double.</param>
/// <param name="max">Maximum value of the double.</param>
/// <returns>A random double.</returns>
public double GetRandomDouble(double min, double max)
{
return Modulo(GetRandomDouble(), max - min + 1) + min;
}
#region Basic Operations
/// <summary>
/// Get the modulo of two double values.
/// </summary>
/// <param name="a">First double value.</param>
/// <param name="b">Second double value</param>
/// <returns>Modulo of two double values.</returns>
private double Modulo(double a, double b)
{
double closest, residue;
if (b == Math.Floor(b))
{
return (a % b);
}
else
{
closest = Math.Round(a / b);
residue = Math.Abs(a - closest * b);
if (residue < Math.Pow(10.0, -14))
{
return 0.0;
}
else
{
return residue * Math.Sign(a);
}
}
}
/// <summary>
/// Get the modulo of two BigInteger values.
/// </summary>
/// <param name="a">First BigInteger value.</param>
/// <param name="b">Second BigInteger value.</param>
/// <returns>Modulo of two BigInteger values.</returns>
private BigInteger Modulo(BigInteger a, BigInteger b)
{
return (BigInteger.Abs(a * b) + a) % b;
}
/// <summary>
/// Get a basic random integer (Int32) value.
/// </summary>
/// <returns>A basic random integer (Int32) value.</returns>
private int GetBasicRandomInt32()
{
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] randomNumber = new byte[4];
rng.GetBytes(randomNumber);
int value = BitConverter.ToInt32(randomNumber, 0);
if (value < 0)
{
value *= -1;
}
return value;
}
/// <summary>
/// Get a basic random unsigned integer (IInt32) value.
/// </summary>
/// <returns>A basic random unsigned integer (UInt32) value.</returns>
private uint GetBasicRandomUInt32()
{
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] randomNumber = new byte[4];
rng.GetBytes(randomNumber);
uint value = BitConverter.ToUInt32(randomNumber, 0);
return value;
}
/// <summary>
/// Get a basic random long (Int64) value.
/// </summary>
/// <returns>A basic random long (Int64) value.</returns>
private long GetBasicRandomInt64()
{
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] randomNumber = new byte[8];
rng.GetBytes(randomNumber);
long value = BitConverter.ToInt64(randomNumber, 0);
if (value < 0)
{
value *= -1;
}
return value;
}
/// <summary>
/// Get a basic random unsigned long (UInt64) value.
/// </summary>
/// <returns>A basic random unsigned long (UInt64) value.</returns>
private ulong GetBasicRandomUInt64()
{
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] randomNumber = new byte[8];
rng.GetBytes(randomNumber);
ulong value = BitConverter.ToUInt64(randomNumber, 0);
return value;
}
/// <summary>
/// Get a basic random short (Int16) value.
/// </summary>
/// <returns>A basic random short (Int16) value.</returns>
private short GetBasicRandomInt16()
{
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] randomNumber = new byte[2];
rng.GetBytes(randomNumber);
short value = BitConverter.ToInt16(randomNumber, 0);
if (value < 0)
{
value *= -1;
}
return value;
}
/// <summary>
/// Get a basic random unsigned short (UInt16) value.
/// </summary>
/// <returns>A basic random unsigned short (UInt16) value.</returns>
private ushort GetBasicRandomUInt16()
{
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] randomNumber = new byte[2];
rng.GetBytes(randomNumber);
ushort value = BitConverter.ToUInt16(randomNumber, 0);
return value;
}
/// <summary>
/// Get a basic random double value.
/// </summary>
/// <returns>A basic random double value.</returns>
private double GetBasicRandomDouble()
{
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] randomNumber = new byte[8];
rng.GetBytes(randomNumber);
double value = BitConverter.ToDouble(randomNumber, 0);
if (value < 0)
{
value *= -1;
}
if (value.ToString().Contains("E"))
{
value = double.Parse(value.ToString().Split('E')[0]);
}
return value;
}
#endregion
}
}