Generate pseudo-random numbers from \(X_{i+1} = (aX_i + c) \bmod{m}\), where \(X_1\) is the initial value (seed).

rand_unit(n = 1, a = 55797, c = 0, m = 4294967296, seed = NULL)

Arguments

n

The desired length of the sequence.

a, c, m

Parameters for the generator (see References for the default values).

seed

The seed. The default is the system time when this function is called for the first time in the current session. For subsequent calls, the last \(X_i\) of the previous call will be used as the default seed.

Value

Random numbers on [0, 1) (i.e., \(X/m\) instead of \(X\)). Note the unit interval is open on the right (excluding 1).

Note

All argument values must be smaller than \(2^{64}\) as they will be coerced to 64-bit integers.

References

Steele, Guy L. Jr.; Vigna, Sebastiano (2022). "Computationally easy, spectrally good multipliers for congruential pseudorandom number generators". Software: Practice and Experience. 52 (2): 443–458.

Examples

rand_unit(10)
#>  [1] 0.5136864 0.1618614 0.3798540 0.7112443 0.2981945 0.3610323 0.5189014
#>  [8] 0.1433405 0.9683106 0.8241010
rand_unit(10, seed = 0)
#>  [1] 0 0 0 0 0 0 0 0 0 0
rand_unit(10, seed = 0)  # identical results
#>  [1] 0 0 0 0 0 0 0 0 0 0
rand_unit(10, seed = Sys.getpid())
#>  [1] 0.95559044 0.07971351 0.77482790 0.07255029 0.08837698 0.17061523
#>  [7] 0.81816954 0.40605874 0.85939165 0.47584042