Pseudo-random numbers on [0, 1) based on a linear congruential generator
Source: R/algorithm.R
rand_unit.RdGenerate pseudo-random numbers from \(X_{i+1} = (aX_i + c) \bmod{m}\), where \(X_1\) is the initial value (seed).
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.5848423 0.4459844 0.5892345 0.5190831 0.2791928 0.1195667 0.4639759
#> [8] 0.4617381 0.6023530 0.4907743
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.40558468 0.40856718 0.82286037 0.14000871 0.06598401 0.70971492
#> [7] 0.96311994 0.20331263 0.23509265 0.46431560