Novetus_src/Novetus/NovetusCore/Classes/CryptoRandom.cs

55 lines
1.6 KiB
C#
Raw Normal View History

#region Usings
using System;
using System.Security.Cryptography;
#endregion
namespace Novetus.Core
{
#region CryptoRandom
public class CryptoRandom : RandomNumberGenerator
{
private static RandomNumberGenerator r;
public CryptoRandom()
{
r = Create();
}
///<param name=”buffer”>An array of bytes to contain random numbers.</param>
public override void GetBytes(byte[] buffer)
{
r.GetBytes(buffer);
}
public override void GetNonZeroBytes(byte[] data)
{
r.GetNonZeroBytes(data);
}
public double NextDouble()
{
byte[] b = new byte[4];
r.GetBytes(b);
return (double)BitConverter.ToUInt32(b, 0) / UInt32.MaxValue;
}
///<param name=”minValue”>The inclusive lower bound of the random number returned.</param>
///<param name=”maxValue”>The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minValue.</param>
public int Next(int minValue, int maxValue)
{
return (int)Math.Round(NextDouble() * (maxValue - minValue - 1)) + minValue;
}
public int Next()
{
return Next(0, int.MaxValue);
}
///<param name=”maxValue”>The inclusive upper bound of the random number returned. maxValue must be greater than or equal 0</param>
public int Next(int maxValue)
{
return Next(0, maxValue);
}
}
#endregion
}