Created
February 28, 2013 23:41
-
-
Save BenMatteson/5061144 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Security.Cryptography; | |
namespace Octgn.Utils | |
{ | |
class CryptoRNG | |
{ | |
RNGCryptoServiceProvider rnd; | |
Byte[] randomBytes = new Byte[4]; | |
public CryptoRNG() | |
{ | |
rnd = new RNGCryptoServiceProvider(); | |
} | |
public UInt32 Next() | |
{ | |
rnd.GetBytes(randomBytes); | |
return BitConverter.ToUInt32(randomBytes, 0); | |
} | |
public int Next(int maxValue) | |
{ | |
return this.Next(0, maxValue); | |
} | |
public int Next(int minValue, int maxValue) | |
{ | |
if (minValue > maxValue) throw new ArgumentOutOfRangeException(); | |
if (minValue == maxValue) return minValue; | |
int diff = maxValue - minValue; | |
double percent = this.NextDouble(); | |
return (int) (percent * diff + minValue); | |
} | |
public double NextDouble() | |
{ | |
return this.Next() / ((double)(UInt32.MaxValue) + 1); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment