Split Geekbot.net into src/Bot, src/Core, and src/Web
This commit is contained in:
parent
7b6dd2d2f9
commit
fc0af492ad
197 changed files with 542 additions and 498 deletions
7
src/Core/RandomNumberGenerator/IRandomNumberGenerator.cs
Normal file
7
src/Core/RandomNumberGenerator/IRandomNumberGenerator.cs
Normal file
|
@ -0,0 +1,7 @@
|
|||
namespace Geekbot.Core.RandomNumberGenerator
|
||||
{
|
||||
public interface IRandomNumberGenerator
|
||||
{
|
||||
int Next(int minValue, int maxExclusiveValue);
|
||||
}
|
||||
}
|
46
src/Core/RandomNumberGenerator/RandomNumberGenerator.cs
Normal file
46
src/Core/RandomNumberGenerator/RandomNumberGenerator.cs
Normal file
|
@ -0,0 +1,46 @@
|
|||
using System;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace Geekbot.Core.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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue