Choosing a Random Number Generator
Numerics.NET provides multiple random number generator algorithms, each with different characteristics and tradeoffs. This guide helps you choose the right generator for your needs.
Quick Recommendations
For most applications, use one of these:
Default choice: Xoshiro256StarStar is fast, high-quality, and the default shared generator in .NET 6+.
NumPy compatibility: Pcg64 or Pcg64Dxsm are the default in NumPy. Use this for reproducible results that match NumPy code.
Parallel computing: Use Numerics.NET's built-in parallel streams with any RNG, or Philox4x64, a counter-based generator ideal for GPU and large-scale parallel work.
Generator Families
Random number generators fall into several families based on their internal structure:
Xoshiro/Xoroshiro Family (Recommended for Most Use)
These generators use XOR-shift-rotate operations and are extremely fast while providing excellent statistical quality:
Xoshiro256StarStar — 256-bit state, highest quality, default in .NET 6+.
Xoshiro256PlusPlus — Alternative output function, slightly faster floating-point generation.
Xoshiro256Plus — Fastest, but lower quality output (avoid for general use).
Xoroshiro128PlusPlus — 128-bit state, very fast, good quality.
Key features: Very fast, support jumping for parallel streams, excellent statistical quality. Use ** or ++ variants for general work.
PCG Family (NumPy Default)
Permuted Congruential Generators combine linear congruential generation with permutation functions:
Pcg64 — Default in NumPy, supports streams, excellent quality.
Pcg64Dxsm — Alternative output function, slightly different characteristics.
Key features: Built-in stream support, NumPy-compatible, good performance, excellent statistical quality. Choose this for NumPy reproducibility.
Counter-Based Generators (Best for GPU/Parallel)
Counter-based generators use encryption-like operations and can seek to any position instantly:
Philox4x64 — Industry standard for GPU computing, supports NumPy compatibility.
Threefry4x64 — Based on Threefish cipher, very high quality.
ChaCha — Cryptographically secure, slowest but highest quality.
Key features: Perfect for GPU computing, instant seeking, built-in independent streams. Choose Philox for GPU work or when you need many independent streams.
Other Generators
Additional generators for specific needs or legacy compatibility:
MersenneTwister64 — Classic generator, widely used historically. Slower than modern alternatives.
SplitMix64 — Simple and fast, used for seeding other generators.
Sfc64 — Small Fast Counting generator, very fast and simple.
Decision Tree
Use this decision tree to select the right generator:
Are you using parallel/multi-threaded code?
If yes, and you need determinism: Use parallel streams. See Parallel Randomness and Independent Streams.
If yes, and you don't need determinism: Use Shared.
Do you need determinism?
If no: Use Shared.
If yes, continue with algorithm selection:
Do you need NumPy-compatible results? Use Pcg64 with SeedProfile.Numpy.
Are you doing GPU or massively parallel computing? Use Philox4x64.
Do you need cryptographic security? Use ChaCha.
Otherwise: Use Xoshiro256StarStar for best overall balance of speed and quality.
Performance Characteristics
Approximate relative performance (higher is better):
Generator | Relative Speed | Statistical Quality | Parallel Support |
|---|---|---|---|
Xoshiro256** | Very Fast | Excellent | Jump-based |
Xoshiro256++ | Very Fast | Excellent | Jump-based |
SplitMix64 | Very Fast | Good | No |
Pcg64 | Fast | Excellent | Stream IDs |
Philox4x64 | Slow | Excellent | Counter-based |
MersenneTwister64 | Fast | Good | No |
ChaCha | Very slow | Cryptographic | Counter-based |
Note: Performance varies based on workload (integer vs. floating-point, bulk vs. scalar). These are approximate guidelines.
Cross-Platform Compatibility
If you need results to match other platforms:
NumPy: Use Pcg64, Pcg64Dxsm, Philox4x64, Sfc64, or MersenneTwister (MT19937) with SeedProfile.Numpy.
.NET: Wrap System.Random instead. See Compatibility and Interop for details on System.Random interop.
C++ std::mt19937_64: MersenneTwister64 (MT19937-64) with SeedProfile.CPlusPlus. Additionally, std::mt19937 corresponds to MersenneTwister (MT19937), and Philox4x64 is proposed for C++26.
Reproducing Reference Implementations
When reproducing results from reference implementations of random number generators, use SeedProfile.Standard to match the original initialization behavior defined by the algorithm's creators. Many generators also provide specialized constructors that implement standard initialization patterns.
Some generators provide specialized constructors that implement standard initialization without requiring explicit profile specification. Refer to the documentation for each generator class for details.
PCG Family
For Pcg64 and Pcg32, use SeedProfile.Standard to match the reference implementation from PCG Random (pcg-random.org). This applies the standard PCG seeding algorithm that combines the seed with stream/sequence identifiers.
Xoshiro/Xoroshiro Family
For generators in the Xoshiro/Xoroshiro family (including Xoshiro256StarStar, Xoshiro256PlusPlus, and Xoroshiro128PlusPlus), use SeedProfile.Standard to match the reference implementations by Blackman and Vigna. The standard profile uses SplitMix64 for state initialization as specified in the original papers.
Mersenne Twister
For MersenneTwister and MersenneTwister64, both SeedProfile.Standard and SeedProfile.CPlusPlus are available. Use SeedProfile.Standard to match the Hiroshima University 2004 reference implementation, or SeedProfile.CPlusPlus to match C++11's std::mt19937 and std::mt19937_64.
Available Random Number Generators
The following table lists all random number generators available in Numerics.NET:
Class | Algorithm | Description |
|---|---|---|
PCG64 XSL-RR | Recommended default. 128-bit state, NumPy-compatible, excellent statistical quality. By Melissa O'Neill. | |
PCG64 DXSM | Best overall statistical quality in PCG family, 128-bit state. By Melissa O'Neill. | |
PCG32 XSH-RR | Fast 32-bit generator with 64-bit state. By Melissa O'Neill. | |
Xoshiro256** | .NET 6+ default, very fast, general-purpose use. By Blackman and Vigna. | |
Xoshiro256++ | Fast, excellent statistical properties for all use cases. By Blackman and Vigna. | |
Xoshiro256+ | Optimized for floating-point, default in Julia and GNU Fortran. By Blackman and Vigna. | |
Xoshiro128** | Fast 32-bit generator, general-purpose. By Blackman and Vigna. | |
Xoshiro128+ | Fast 32-bit generator for floating-point. By Blackman and Vigna. | |
Xoroshiro128++ | Faster than xoshiro256, smaller state. By Blackman and Vigna. | |
Philox 4x64 | Counter-based, ideal for parallel computing. By Salmon et al. | |
Philox 4x32 | Counter-based 32-bit generator. By Salmon et al. | |
Threefry 4x64 | Counter-based using Threefish cipher. By Salmon et al. | |
ChaCha20 | Cryptographically secure, suitable for security-critical applications. By Bernstein. | |
MT19937 | Classic generator with 2^19937-1 period. By Nishimura and Matsumoto. Legacy compatibility. | |
MT19937-64 | 64-bit Mersenne Twister, C++11 compatible. By Nishimura and Matsumoto. | |
SFC64 | Small Fast Chaotic, very fast with excellent quality. By Doty-Humphrey. | |
WyRand | Extremely fast hash-based generator. By Wang Yi. | |
SplitMix64 | Fast, primarily for seeding other generators. By Steele and Lea. | |
RANLUX | High-quality with adjustable luxury level, 48-bit blocks. By Lüscher/James. | |
RANLUX-24 | 24-bit variant of RANLUX. By Lüscher/James. | |
GFSR | Generalized Feedback Shift Register. Legacy compatibility. By Ziff. |
For new applications, we recommend Pcg64 or Xoshiro256StarStar for general use, and Philox4x64 for parallel computing.
Next Steps
Now that you've chosen a generator:
Introduction to Random Sources — Learn how to create and use your chosen generator.
Parallel Randomness and Independent Streams — Use your generator in parallel code.
Compatibility and Interop — Understand reproducibility constraints.
References
David Blackman and Sebastiano Vigna, “Scrambled Linear Pseudorandom Number Generators”, , Vol. 47, No. 4 (Dec. 2021), Article 36.
Donald E. Knuth, (Vol 2, 3rd Ed, 1997), Addison-Wesley.
M. Loescher, “A portable high-quality random number generator for lattice field theory calculations”, , 79 (1994) 100-110.
Makoto Matsumoto and Takuji Nishimura, “Mersenne Twister: A 623-dimensionally equidistributed uniform pseudorandom number generator”. , Vol. 8, No. 1 (Jan. 1998), Pages 3-30
Melissa E. O'Neill, “PCG: A Family of Simple Fast Space-Efficient Statistically Good Algorithms for Random Number Generation”, , 2014.
John K. Salmon, Mark A. Moraes, Ron O. Dror, and David E. Shaw, “Parallel Random Numbers: As Easy as 1, 2, 3”, (SC '11), Article 16, 2011. (Random123 website)
Robert M. Ziff, “Four-tap shift-register-sequence random-number generators”, , 12(4), Jul/Aug 1998, pp 385-392.