API FastRandom - shmellyorc/Box GitHub Wiki
FastRandom
Namespace: Box.Utils
A fast, non-cryptographic PRNG based on xoroshiro128+. Suitable for games and simulations where speed is critical. Provides global Instance
seeded from system entropy or custom seed.
Constructors
internal FastRandom()
Initialize with a seed derived from Environment.TickCount64
via cryptographic-quality mixing. Automatically sets Instance
if not already assigned.
internal FastRandom(ulong seed)
Initialize with a user-provided 64-bit seed. Instance
is set on first construction.
Properties
Name | Type | Description |
---|---|---|
static FastRandom Instance |
FastRandom |
Singleton instance, set on first constructed instance. |
Methods
Signature | Description |
---|---|
void SetSeed(ulong seed) |
Reseed the generator with a new 64-bit value. |
int NextInt() |
Return a non-negative int in [0, int.MaxValue] . |
int NextInt(int max) |
Return int in [0, max) . max must be > 0. |
int NextInt(int min, int max) |
Return int in [min, max) . min < max required. |
long NextLong() |
Return a non-negative long in [0, long.MaxValue] . |
long NextLong(long max) |
Return long in [0, max) . max must be > 0. |
long NextLong(long min, long max) |
Return long in [min, max) . min < max required. |
double NextDouble() |
Return double in [0.0, 1.0) . |
double NextDouble(double max) |
Return double in [0.0, max) . max must be > 0. |
double NextDouble(double min, double max) |
Return double in [min, max) . min < max required. |
float NextFloat() |
Return float in [0.0f, 1.0f) . |
float NextFloat(float max) |
Return float in [0.0f, max) . max must be > 0. |
float NextFloat(float min, float max) |
Return float in [min, max) . min < max required. |
bool NextBool() |
Return a random boolean (true or false ). |
int Range(int min, int max) |
Return int in inclusive range [min, max] . min <= max required. |
long Range(long min, long max) |
Return long in inclusive range [min, max] . min <= max required. |
double Range(double min, double max) |
Return double in half-open range [min, max) . min < max required. |
float Range(float min, float max) |
Return float in half-open range [min, max) . min < max required. |
Usage Example
// Ensure the global instance is initialized
var rand = new FastRandom();
// Reseed if desired
rand.SetSeed(123456789UL);
int a = rand.NextInt(); // e.g. 123456
int b = rand.NextInt(10); // [0,10)
int c = rand.NextInt(5, 15); // [5,15)
long x = rand.NextLong(); // non-negative long
bool flag = rand.NextBool(); // true or false
float f = rand.NextFloat(0.5f, 2.0f); // [0.5,2.0)
double d = rand.Range(0.0, 100.0); // [0.0,100.0)