Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Security.Cryptography;
- public class SecureRandom : RandomNumberGenerator
- {
- private readonly RandomNumberGenerator rng = new RNGCryptoServiceProvider();
- public int Next()
- {
- var data = new byte[sizeof(int)];
- rng.GetBytes(data);
- return BitConverter.ToInt32(data, 0) & (int.MaxValue - 1);
- }
- public int Next(int maxValue)
- {
- return Next(0, maxValue);
- }
- public int Next(int minValue, int maxValue)
- {
- if (minValue > maxValue)
- {
- throw new ArgumentOutOfRangeException();
- }
- return (int)Math.Floor((minValue + ((double)maxValue - minValue) * NextDouble()));
- }
- public double NextDouble()
- {
- var data = new byte[sizeof(uint)];
- rng.GetBytes(data);
- var randUint = BitConverter.ToUInt32(data, 0);
- return randUint / (uint.MaxValue + 1.0);
- }
- public double NextDouble(double minimum, double maximum)
- {
- return NextDouble() * (maximum - minimum) + minimum;
- }
- public override void GetBytes(byte[] data)
- {
- rng.GetBytes(data);
- }
- public override void GetNonZeroBytes(byte[] data)
- {
- rng.GetNonZeroBytes(data);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement