Skip to content

FastRandom

Shmellyorc edited this page Aug 31, 2026 · 2 revisions

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.


Overview

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

Properties

Property Description
Shared Gets a thread-local FastRandom instance for the current thread

Constructors

// 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;

Methods

Next

Generates a random non-negative integer.

var random = FastRandom.Shared;

// Random integer between 0 and int.MaxValue - 1
int value = random.Next();

Next(maxValue)

Generates a random integer between 0 (inclusive) and the specified maximum (exclusive).

// Random integer between 0 and 99
int value = random.Next(100);

Next(minValue, maxValue)

Generates a random integer between the specified minimum (inclusive) and maximum (exclusive).

// Random integer between 10 and 99
int value = random.Next(10, 100);

NextBoolean

Generates a random boolean value.

bool value = random.NextBoolean();  // true or false

NextFloat

Generates a random float between 0 (inclusive) and 1 (exclusive).

float value = random.NextFloat();  // 0.0 to 1.0

NextFloat(maxValue)

Generates a random float between 0 (inclusive) and the specified maximum (exclusive).

float value = random.NextFloat(100f);  // 0.0 to 100.0

NextFloat(minValue, maxValue)

Generates a random float between the specified minimum (inclusive) and maximum (exclusive).

float value = random.NextFloat(10f, 100f);

NextDouble

Generates a random double between 0 (inclusive) and 1 (exclusive).

double value = random.NextDouble();  // 0.0 to 1.0

NextDouble(maxValue)

Generates a random double between 0 (inclusive) and the specified maximum (exclusive).

double value = random.NextDouble(100.0);

NextDouble(minValue, maxValue)

Generates a random double between the specified minimum (inclusive) and maximum (exclusive).

double value = random.NextDouble(10.0, 100.0);

RangeInt

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);

RangeFloat

Generates a random float between the specified minimum (inclusive) and maximum (inclusive).

float value = random.RangeFloat(10f, 100f);

RangeDouble

Generates a random double between the specified minimum (inclusive) and maximum (inclusive).

double value = random.RangeDouble(10.0, 100.0);

Examples

Basic Usage

var random = FastRandom.Shared;

int diceRoll = random.RangeInt(1, 6);
float health = random.NextFloat(50f, 100f);
bool criticalHit = random.NextBoolean();

Deterministic Sequences

// 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

Thread Safety

// 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
});

Performance

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.


Summary

Feature Support
Xorshift128 algorithm
Thread-local instances
Deterministic seeding
Integer generation
Float generation
Double generation
Boolean generation
Inclusive ranges
Exclusive ranges

Back to Math Library

Clone this wiki locally