FastRng.ThreadSafe 1.0.5

There is a newer version of this package available.
See the version list below for details.
dotnet add package FastRng.ThreadSafe --version 1.0.5
                    
NuGet\Install-Package FastRng.ThreadSafe -Version 1.0.5
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FastRng.ThreadSafe" Version="1.0.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FastRng.ThreadSafe" Version="1.0.5" />
                    
Directory.Packages.props
<PackageReference Include="FastRng.ThreadSafe" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add FastRng.ThreadSafe --version 1.0.5
                    
#r "nuget: FastRng.ThreadSafe, 1.0.5"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package FastRng.ThreadSafe@1.0.5
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=FastRng.ThreadSafe&version=1.0.5
                    
Install as a Cake Addin
#tool nuget:?package=FastRng.ThreadSafe&version=1.0.5
                    
Install as a Cake Tool

FastRng.ThreadSafe πŸš€

![Build & Publish to NuGet] NuGet Version License: MIT

An ultra-high-performance, non-blocking, thread-safe Pseudo-Random Number Generator (PRNG) engineered explicitly for modern multithreaded .NET applications (optimized for .NET 10), network game servers, and casino systems.

FastRng inherits directly from System.Random, acting as a drop-in replacement that eliminates multi-threaded race conditions, lock contentions, and mathematical bias.

This generator features a multi-layered, structural state permutation engine based on isolated RC4-style layers. It operates on a continuous 1.5KB flat block of memory perfectly aligned to sit inside the processor's L1 Data Cache. By completely omitting bounds-checking using Unsafe memory mapping and avoiding cross-layer state leakage, it preserves flawless 1-to-1 array permutations indefinitely.


Mathematical Foundations

1. Permutation Stability and State Space

The internal state of the generator consists of 6 isolated layers ($M_0, M_1, \dots, M_5$), where each layer is an array containing a strict permutation of the byte space $\mathbb{Z}_{256} = {0, 1, \dots, 255}$.

Because the algorithm relies strictly on transpositions (swaps) inside individual boundaries: $$\forall m \in, \quad \sum_{k=0}^{255} M_m[k] = 32640$$

No values are cloned, and no values are deleted. The card deck is always pristine. The theoretical total state space (period bounds) of the system is defined by the permutations of all layers and index offsets: $$\Omega = (256!)^6 \times 256^2 \approx 10^{3044}$$ This makes structural cycle repetition mathematically impossible over any practical software lifecycle.

2. Cascading Diffusion and Chaotic Trajectory

The generator utilizes a forward feedback mechanism where the byte value extracted from layer $n$ acts as an immediate pointer index for layer $n+1$. The recursion depth $L$ is a dynamic variable determined on-the-fly by the state entropy: $$L = (V_0 \pmod 4) + 3 \quad \implies \quad L \in [3, 6]$$

The transitional stepping rules for each layer $m$ satisfy: $$j_m = (j_{m-1} + M_m[i_m]) \pmod{256}$$ $$\text{Swap}(M_m[i_m], M_m[j_m])$$ $$\text{Output } V_m = M_m[(M_m[i_m] + M_m[j_m]) \pmod{256}]$$

By passing $V_m$ straight to the next layer as the pointer index ($i_{m+1} = V_m$), the algorithm creates an algorithmic Avalanche Effect. A single bit flipped at layer 0 yields completely uncorrelated trajectories across the deep matrix blocks.

3. Statistical Validation

Chi-Squared ($\chi^2$) Uniformity Analysis

The uniform probability distribution over long-range execution paths is verified using Pearson's $\chi^2$ Goodness-of-Fit test over a grid of $65,536$ coordinate pairs: $$\chi^2 = \sum_{r=0}^{255} \sum_{c=0}^{255} \frac{(A_{r,c} - E)^2}{E}$$ Where $A_{r,c}$ is the actual count of the sequential transition $r \to c$, and $E$ is the expected mathematical uniform mean ($\approx 152.58$ over 10,000,000 samples).

The generator consistently yields $\chi^2 \in [64000, 67000]$ for $65,535$ degrees of freedom at a $99%$ confidence interval, perfectly matching the natural randomness curves of physical matter.

Gaussian Curve Behavior (Pairwise Cluster Bounds)

Under a high-volume sample load, the cell counts naturally obey the Central Limit Theorem. The variation around the expected mean behaves as a classic Gaussian bell curve with a standard deviation of: $$\sigma = \sqrt{E} = \sqrt{152.58} \approx 12.35$$

The absolute cluster ceiling for natural random path clustering hits a predictable limit of $+4.5\sigma \approx 208$ transitions per cell. This structure preserves organic randomness properties, completely passing heavy spatial tests.


✨ Features

  • πŸ›‘οΈ 100% Thread-Safe Isolation: Utilizes advanced thread-local processing registers to completely eliminate resource locking contention across heavily concurrent pipelines.
  • 🎰 Casino-Grade Uniformity: Built-in uniform distribution mechanics using mathematical rejection sampling to completely eliminate floating-point truncation and modulo biases.
  • πŸ”€ Advanced Array & Span Shuffling: Implements an unbiased Fisher-Yates shuffle engine optimized for card decks and game reels.
  • βš–οΈ Weighted Index Selection: High-speed weighted distribution support crucial for Slot Machine RTP (Return to Player) setups, loot drops, and probability engines.
  • πŸ”„ Deep Integration: Fully compatible upcast mappingβ€”if third-party frameworks cast this library to System.Random, our core overridden distribution hooks continue to handle the execution.

πŸ’» Installation

Install via the NuGet Package Manager Console:

Install-Package FastRng.ThreadSafe

Or via the .NET Core CLI:

dotnet add package FastRng.ThreadSafe

πŸš€ Quick Start & Usage Examples

1. Basic Generation & Drop-In Replacement

Since FastRng overrides System.Random, you can initialize it once and share it safely across all background workers:

using FastRng.ThreadSafe;

// Safe to share across multiple tasks and threads simultaneously
Random rng = FastRng.Instance;

// Generates a perfectly uniform integer: 0 to 36 inclusive (e.g., European Roulette Wheel)
int rouletteSpin = rng.Next(0, 37); 

double probability = rng.NextDouble();
byte randomByte = rng.NextByte();

2. Unbiased Card / Array Shuffling

Perfect for card games (Blackjack, Poker) or generating random paths without duplicate items.

var rng = FastRng.Instance;

// Generate a classic card deck
int[] playingCards = Enumerable.Range(0, 52).ToArray();

// Shuffles the data in-place with exactly uniform permutation probabilities
rng.Shuffle(playingCards);

3. Casino Weighted Selection (Slot Machine Reel Strip)

Ideal for game mechanics where different symbols or rewards have varying likelihoods of appearing.

var rng = FastRng.Instance;

// Index 0 (Jackpot) = 1% chance
// Index 1 (Medium Reward) = 19% chance
// Index 2 (Common Loss) = 80% chance
int[] prizeWeights = { 10, 190, 800 };

// Automatically calculates total ranges and returns the winning index selection safely
int winningPrizeIndex = rng.NextWeightedIndex(prizeWeights);

πŸ”¬ Statistical Security & Fairness Validation

Every release of FastRng is rigorously audited by our automated testing pipeline against industry-standard mathematical and entropic evaluations:

  • πŸ“Š Multidimensional Independence: Validated using a 2D Chi-Squared Matrix Transition Grid ($256 \times 256$ state paths over 10,000,000 continuous draws) to prove consecutive draws have zero serial memory correlation.
  • πŸ›‘οΈ NIST SP 800-22 Frequency (Monobit): PASSED (Ensures a true 50/50 balance of density distribution between binary bitstreams).
  • πŸ›‘οΈ NIST SP 800-22 Runs Test: PASSED (Confirms bit transformations oscillate at a natural, non-repeating structural tempo).

πŸ“„ License

This project is licensed under the terms of the MIT License. The text of the license is included in full inside the root LICENSE file.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.7 129 7/7/2026
1.0.5 122 6/27/2026
1.0.4 110 6/20/2026
1.0.0 124 6/17/2026