-
Notifications
You must be signed in to change notification settings - Fork 1
FastRandom
FastRandom is a high-performance, thread-safe random number generator using the Xorshift128 algorithm. It is significantly faster than System.Random and provides per-thread instances to avoid contention in multi-threaded scenarios.
| Feature | Description |
|---|---|
| Algorithm | Xorshift128 |
| Performance | Significantly faster than System.Random
|
| Thread Safety | Thread-local instances via Shared
|
| Deterministic | Seeded instances produce reproducible sequences |
| Distribution | Uniform distribution for all methods |
| Property | Description |
|---|---|
Shared |
Gets a thread-local FastRandom instance for the current thread |
// Create with a seed for deterministic sequences
var random = new FastRandom(12345);
int value = random.Next(); // Always produces the same sequence
// Create with a seed derived from system time (default)
var random2 = new FastRandom();
// Use the shared thread-local instance
var shared = FastRandom.Shared;Generates a random non-negative integer.
var random = FastRandom.Shared;
// Random integer between 0 and int.MaxValue - 1
int value = random.Next();Generates a random integer between 0 (inclusive) and the specified maximum (exclusive).
// Random integer between 0 and 99
int value = random.Next(100);Generates a random integer between the specified minimum (inclusive) and maximum (exclusive).
// Random integer between 10 and 99
int value = random.Next(10, 100);Generates a random boolean value.
bool value = random.NextBoolean(); // true or falseGenerates a random float between 0 (inclusive) and 1 (exclusive).
float value = random.NextFloat(); // 0.0 to 1.0Generates a random float between 0 (inclusive) and the specified maximum (exclusive).
float value = random.NextFloat(100f); // 0.0 to 100.0Generates a random float between the specified minimum (inclusive) and maximum (exclusive).
float value = random.NextFloat(10f, 100f);Generates a random double between 0 (inclusive) and 1 (exclusive).
double value = random.NextDouble(); // 0.0 to 1.0Generates a random double between 0 (inclusive) and the specified maximum (exclusive).
double value = random.NextDouble(100.0);Generates a random double between the specified minimum (inclusive) and maximum (exclusive).
double value = random.NextDouble(10.0, 100.0);Generates a random integer between the specified minimum (inclusive) and maximum (inclusive).
// Random integer between 10 and 100 inclusive
int value = random.RangeInt(10, 100);Generates a random float between the specified minimum (inclusive) and maximum (inclusive).
float value = random.RangeFloat(10f, 100f);Generates a random double between the specified minimum (inclusive) and maximum (inclusive).
double value = random.RangeDouble(10.0, 100.0);var random = FastRandom.Shared;
int diceRoll = random.RangeInt(1, 6);
float health = random.NextFloat(50f, 100f);
bool criticalHit = random.NextBoolean();// Seeded random for reproducible results
var random1 = new FastRandom(42);
var random2 = new FastRandom(42);
int a = random1.Next(); // Same value
int b = random2.Next(); // Same value
// Useful for testing and procedural generation// Each thread gets its own instance automatically
var random = FastRandom.Shared; // Thread-local
// No locks required, no contention
Parallel.For(0, 100, i =>
{
int value = FastRandom.Shared.Next(100);
// Process value
});| Method | Performance |
|---|---|
Next() |
Fast |
Next(max) |
Fast |
NextFloat() |
Fast |
NextDouble() |
Fast |
NextBoolean() |
Fast |
FastRandom is significantly faster than System.Random and uses no locks when accessed through the Shared property.
| Feature | Support |
|---|---|
| Xorshift128 algorithm | ✅ |
| Thread-local instances | ✅ |
| Deterministic seeding | ✅ |
| Integer generation | ✅ |
| Float generation | ✅ |
| Double generation | ✅ |
| Boolean generation | ✅ |
| Inclusive ranges | ✅ |
| Exclusive ranges | ✅ |
- MathHelper
- FileHelper
- HashHelper
- JsonHelper
- MapHelper
- TextHelper
- SoundHelper
- AlignHelpers
- Instance Helper
- Enum Extensions
- String Extensions
- Int Extensions
- Float Extensions
- IEnumerable Extensions
- Random Extensions
- Sound Extensions
- Font Extensions