Created
May 28, 2015 00:29
-
-
Save rakkarage/37ab0b4710869787e1bc 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.Security.Cryptography; | |
using UnityEngine; | |
public static class Random | |
{ | |
private static int _offset = 0; | |
private static byte[] _buffer = new byte[1024]; | |
private static RNGCryptoServiceProvider _random = new RNGCryptoServiceProvider(); | |
private static void Fill() | |
{ | |
_random.GetBytes(_buffer); | |
_offset = 0; | |
} | |
public static int Next() | |
{ | |
if (_offset >= _buffer.Length) | |
Fill(); | |
var value = BitConverter.ToInt32(_buffer, _offset) & 0x7fffffff; | |
_offset += sizeof(int); | |
return value; | |
} | |
public static int Next(int max) | |
{ | |
return Next() % max; | |
} | |
public static int Next(int min, int max) | |
{ | |
if (max < min) | |
throw new ArgumentOutOfRangeException(); | |
return min + Next(max - min); | |
} | |
public static int NextEven(int min, int max) | |
{ | |
return Next(min / 2, max / 2) * 2; | |
} | |
public static int NextOdd(int min, int max) | |
{ | |
return Next(min, max) + 1; | |
} | |
public static double NextDouble() | |
{ | |
return (double)Next() / int.MaxValue; | |
} | |
public static double NextDouble(double max) | |
{ | |
return NextDouble() * max; | |
} | |
public static double NextDouble(double min, double max) | |
{ | |
return min + NextDouble() * (max - min); | |
} | |
public static bool NextBool() | |
{ | |
return NextDouble() > 0.5; | |
} | |
public static bool NextPercent(double target) | |
{ | |
return NextDouble() < target; | |
} | |
public static Color NextColor() | |
{ | |
return new Color((float)NextDouble(), (float)NextDouble(), (float)NextDouble()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment