Menu

[59aa80]: / FeedbackGeneratorQQ / Utils / RandomGenerator.cs  Maximize  Restore  History

Download this file

46 lines (41 with data), 1.4 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
namespace FeedbackGeneratorQQ.Utils
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
public static class RandomGenerator
{
private static readonly RNGCryptoServiceProvider CryptRng = new RNGCryptoServiceProvider();
const long _max = (1 + (long)uint.MaxValue);
/// <summary>
/// Gets random integer number from the given interval
/// </summary>
/// <param name="minValue"> Min value (including) </param>
/// <param name="maxValue"> Max value (excluding) </param>
/// <returns> Returns random integer number </returns>
public static int GetRandomNumber(int minValue, int maxValue)
{
if (minValue == maxValue)
{
return minValue;
}
byte[] bytes = new byte[4];
long diff = maxValue - minValue;
while (true)
{
CryptRng.GetBytes(bytes);
uint rand = BitConverter.ToUInt32(bytes, 0);
long remainder = _max % diff;
if (rand < _max - remainder)
{
return (int)(minValue + (rand % diff));
}
}
}
public static IList<T> Shuffle<T>(this IList<T> list)
{
return list.OrderBy(a => GetRandomNumber(0, list.Count * 3)).ToList();
}
}
}
MongoDB Logo MongoDB