mcg59

Crates.iomcg59
lib.rsmcg59
version0.9.4
created_at2025-05-24 21:52:27.93384+00
updated_at2025-05-26 18:17:17.615194+00
descriptionMCG59 random number generator
homepagehttps://gitlab.com/hsn10/mcg59
repositoryhttps://gitlab.com/hsn10/mcg59.git
max_upload_size
id1687771
size30,787
Radim Kolar (hsn10)

documentation

README

MCG59 random number generator

Unlicense Crates.io MSRV 1.56 Safe Rust Dependency status Documentation Lines of code Downloads

59-bit Multiplicative congruential pseudo random number generator

Generator parameters are modulus m = 2^59, multiplier a = 13^13, c = 0. Period is 2^57, output have 59bits.

Generator needs to be seeded with nonzero, odd number. Provided function clamp_seed will take care of that.

Lower bits have low randomness and should be discarded. Highest 27 bits are top quality, passing known statistical tests. This is enough bits for representing fp32 [0,1).

You can take more bits for progresively worse statistic results. Taking 40 bits of MCG59 still beats MCG31 (minstd) generator. Because MCG59 have larger period as well, it is suitable minstd replacement.

Functions

Generator natively generates 59 output bits with 2^57 period.

Conversion functions are provided for converting generated i64 value into array 3 or 5 bytes, i32, u32, i16, u16, f32, f64 discarding lowest bits.

Another function is provided for filling byte slice using 5 bytes chunks.

MCG59 Generator usage

  1. Intel Python mkl_random library
  2. Included in Math.dot.Net
  3. Intel oneMKL
  4. NAG numerical libraries - NAG Fortran Library, NAG C Library.
  5. Arm Performance Libraries

Spectral test

2d 3d 4d 5d 6d 7d 8d 9d
0.8423 0.7288 0.7426 0.5771 0.6351 0.5217 0.5455 0.56

Value 0.75 or higher is considered good enough to pass spectral test. Spectral test do not include discarding lower bits.

Generator analysis

  1. https://www.intel.com/content/www/us/en/docs/onemkl/developer-reference-vector-statistics-notes/2021-1/mcg59.html

No copyright

This is free and unencumbered software released into the public domain.

You may use, modify, distribute, and contribute to this code without restriction. To the extent possible under law, the author(s) of this work waive all copyright and related rights.

Licensed under CC0-1.0 OR Unlicense.

Unlicense logo

Commit count: 0

cargo fmt