Learn R Programming

xfun (version 0.52)

rand_unit: Pseudo-random numbers on [0, 1) based on a linear congruential generator

Description

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

Usage

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

Value

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

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.

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

Run this code
rand_unit(10)
rand_unit(10, seed = 0)
rand_unit(10, seed = 0)  # identical results
rand_unit(10, seed = Sys.getpid())

Run the code above in your browser using DataLab