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:

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:

  1. Are you using parallel/multi-threaded code?

  2. Do you need determinism?

    • If no: Use Shared.

    • If yes, continue with algorithm selection:

      1. Do you need NumPy-compatible results? Use Pcg64 with SeedProfile.Numpy.

      2. Are you doing GPU or massively parallel computing? Use Philox4x64.

      3. Do you need cryptographic security? Use ChaCha.

      4. 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:

  Note

While random streams are reproducible across versions when using the same RNG algorithm and seed profile, sampling from specific probability distributions may not be. The variety of sampling algorithms and potential algorithmic improvements make strict distribution sampling reproducibility impractical to guarantee across library versions.

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.

  Important

When you specify the Standard seed profile, the algorithm's seeding method must be able to handle the requested configuration. If the options are incompatible with the generator's requirements (e.g., insufficient seed length, unsupported stream configuration), an exception will be thrown. Always ensure that your seed and stream parameters meet the requirements of the chosen generator and seed profile.

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

PCG64 XSL-RR

Recommended default. 128-bit state, NumPy-compatible, excellent statistical quality. By Melissa O'Neill.

Pcg64Dxsm

PCG64 DXSM

Best overall statistical quality in PCG family, 128-bit state. By Melissa O'Neill.

Pcg32

PCG32 XSH-RR

Fast 32-bit generator with 64-bit state. By Melissa O'Neill.

Xoshiro256StarStar

Xoshiro256**

.NET 6+ default, very fast, general-purpose use. By Blackman and Vigna.

Xoshiro256PlusPlus

Xoshiro256++

Fast, excellent statistical properties for all use cases. By Blackman and Vigna.

Xoshiro256Plus

Xoshiro256+

Optimized for floating-point, default in Julia and GNU Fortran. By Blackman and Vigna.

Xoshiro128StarStar

Xoshiro128**

Fast 32-bit generator, general-purpose. By Blackman and Vigna.

Xoshiro128Plus

Xoshiro128+

Fast 32-bit generator for floating-point. By Blackman and Vigna.

Xoroshiro128PlusPlus

Xoroshiro128++

Faster than xoshiro256, smaller state. By Blackman and Vigna.

Philox4x64

Philox 4x64

Counter-based, ideal for parallel computing. By Salmon et al.

Philox4x32

Philox 4x32

Counter-based 32-bit generator. By Salmon et al.

Threefry4x64

Threefry 4x64

Counter-based using Threefish cipher. By Salmon et al.

ChaCha

ChaCha20

Cryptographically secure, suitable for security-critical applications. By Bernstein.

MersenneTwister

MT19937

Classic generator with 2^19937-1 period. By Nishimura and Matsumoto. Legacy compatibility.

MersenneTwister64

MT19937-64

64-bit Mersenne Twister, C++11 compatible. By Nishimura and Matsumoto.

Sfc64

SFC64

Small Fast Chaotic, very fast with excellent quality. By Doty-Humphrey.

WyRand

WyRand

Extremely fast hash-based generator. By Wang Yi.

SplitMix64

SplitMix64

Fast, primarily for seeding other generators. By Steele and Lea.

RanLux

RANLUX

High-quality with adjustable luxury level, 48-bit blocks. By Lüscher/James.

RanLux24

RANLUX-24

24-bit variant of RANLUX. By Lüscher/James.

GFSR

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:

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.

See Also

Other Resources