Add a better random number generator

This commit is contained in:
runebaas 2019-05-11 01:18:22 +02:00
parent fe08eee049
commit 4143180b42
No known key found for this signature in database
GPG key ID: 2677AF508D0300D6
8 changed files with 85 additions and 9 deletions

View file

@ -0,0 +1,7 @@
namespace Geekbot.net.Lib.RandomNumberGenerator
{
public interface IRandomNumberGenerator
{
int Next(int minValue, int maxExclusiveValue);
}
}

View file

@ -0,0 +1,46 @@
using System;
using System.Security.Cryptography;
namespace Geekbot.net.Lib.RandomNumberGenerator
{
public class RandomNumberGenerator : IRandomNumberGenerator
{
readonly RNGCryptoServiceProvider csp;
public RandomNumberGenerator()
{
csp = new RNGCryptoServiceProvider();
}
public int Next(int minValue, int maxExclusiveValue)
{
if (minValue >= maxExclusiveValue)
{
throw new ArgumentOutOfRangeException("minValue must be lower than maxExclusiveValue");
}
var diff = (long)maxExclusiveValue - minValue;
var upperBound = uint.MaxValue / diff * diff;
uint ui;
do
{
ui = GetRandomUInt();
} while (ui >= upperBound);
return (int)(minValue + (ui % diff));
}
private uint GetRandomUInt()
{
var randomBytes = GenerateRandomBytes(sizeof(uint));
return BitConverter.ToUInt32(randomBytes, 0);
}
private byte[] GenerateRandomBytes(int bytesNumber)
{
var buffer = new byte[bytesNumber];
csp.GetBytes(buffer);
return buffer;
}
}
}