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();
}
}
}