Learn R Programming

simEd (version 2.0.1)

lehmer: Lehmer Generator Visualization

Description

This function animates the processes of a basic Lehmer pseudo-random number generator (PRNG). Also known in the literature as a multiplicative linear congruential generator (MLCG), the generator is based on the formula:

$$X_{k+1} \equiv a \cdot X_k \pmod{m}$$

where 'm' is the prime modulus, 'a' is the multiplier chosen from {1, m-1}, and 'X_0' is the initial seed chosen from {1, m-1}. The random numbers generated in (0,1) are X_{k+1}/m.

Usage

lehmer(
  a = 13,
  m = 31,
  seed = 1,
  animate = TRUE,
  numSteps = NA,
  title = NA,
  showTitle = TRUE,
  plotDelay = -1
)

Value

the entire period from the PRNG cycle, as a vector of integers in {1, m-1}.

Arguments

a

multiplier in MLCG equation.

m

prime modulus in MLCG equation.

seed

initial seed for the generator, i.e., the initial value X_0

animate

should the visual output be displayed.

numSteps

number of steps to animate; default value is Inf if plotDelay is -1, or the size of the period otherwise. Ignored if animate is false.

title

optional title to display in plot (NA uses default title)

showTitle

if TRUE, display title in the main plot.

plotDelay

wait time between transitioning; -1 (default) for interactive mode, where the user is queried for input to progress.

References

Lehmer, D.H. (1951). Mathematical Models in Large-Scale Computing Units. Ann. Comput. Lab. Harvard University, 26, 141-146.

Examples

Run this code
 # Default case (m, a = 31, 13); small full period
 lehmer(plotDelay = 0, numSteps = 16)
 lehmer(numSteps = 10, plotDelay = 0.1)   # auto-advance mode

 if (interactive()) {
   lehmer(plotDelay = -1)  # plotDelay -1 uses interactive mode
 }
 
 # multiplier producing period of length 5, with different seeds
 lehmer(a = 8, m = 31, seed = 1, numSteps = 5, plotDelay = 0.1)
 lehmer(a = 8, m = 31, seed = 24, numSteps = 5, plotDelay = 0.1)

 # degenerate cases where seed does not appear in the final period
 lehmer(a = 12, m = 20, seed = 7, numSteps = 4, plotDelay = 0.1)  # length  4
 lehmer(a = 4, m = 6, seed = 1, numSteps = 1, plotDelay = 0.1)  # length 1
 

Run the code above in your browser using DataLab