Docsity

Linear Congruential Generators (LCG) Explained with Code Examples, Lecture notes of Accounting

Toggle sidebar
bg1
Review the additional notes for M1L7 More Baby Examples and M2L6 Simulating Random
Variables.
LCG Trivial Example
The LCG starts repeating at which means it has a cycle length/period of .
Also note that
And so on.
LCG Trivial Example R Code
# Initialize the seed and parameters
X <- numeric(10)
X[1] <- 0
a <- 5
c <- 3
m <- 8
# Use the LCG to generate random numbers
for (i in 2:10) {
X[i] <- (a*X[i-1] + c) %% m
}
# Print the first 9 numbers
print(X[1:10])
## [1] 0 3 2 5 4 7 6 1 0 3
pf3
pf4
pf5
pf8

Partial preview of the text

Download Linear Congruential Generators (LCG) Explained with Code Examples and more Lecture notes Accounting in PDF only on Docsity!

Review the additional notes for M1L7 More Baby Examples and M2L6 Simulating Random Variables.

LCG Trivial Example

The LCG starts repeating at which means it has a cycle length/period of.

Also note that

And so on.

LCG Trivial Example R Code

# Initialize the seed and parameters X <- numeric( 10 ) X[ 1 ] <- 0 a <- 5 c <- 3 m <- 8

# Use the LCG to generate random numbers for (i in 2 : 10 ) { X[i] <- (a*X[i-1] + c) %% m }

# Print the first 9 numbers print(X[ 1 : 10 ])

[1] 0 3 2 5 4 7 6 1 0 3

# Plot the pairs (X_i, X{i-1})_ plot(X[ 1 : 9 ], X[ 2 : 10 ], type = "n", xlab = "X_{i-1}", ylab = "X_i", main = "Plot of (X_i, X_{i-1})")

# Add points, labels and lines for (i in 1 : 9 ) { points(X[i], X[i+ 1 ], col = "blue", pch = 19 ) text(X[i], X[i+ 1 ], labels = paste("(", X[i], ",", X[i+ 1 ], ")", sep = ""), pos = 4 , cex = 0.7) }

LCG Trivial Example Python Code

Python Notebook

LCG Better Example

See M2L6 Simulating Random Variables, M1L8 Generating Randomness, specifically the section on Unif(0, 1) PRNs and M1L7 More Baby Examples, specifically the section Evil Random Numbers.

LCG Better Example Python Code

Python Notebook

The Infamous RANDU generator

RANDU is a linear congruential generator (LCG) for generating pseudorandom numbers. It has been used in many systems since the 1960s. However, it is now considered to be flawed and is not recommended for use in most applications.

The problem with RANDU is that it has a poor choice of parameters. Specifically, when three consecutive numbers generated by RANDU are used as coordinates in three-dimensional space, all the points lie on 15 parallel planes. This is a serious violation of the randomness property expected of a good pseudorandom number generator.

This issue arises from the lack of full-period behavior in the generator. A full-period LCG, if properly implemented, can potentially generate m different numbers before repeating, where m is the modulus. However, not all LCGs achieve this full period. The number and distribution of these periods, which are essentially the cycles in the generated sequence, define the quality of the generator.

The choice of the multiplier (65539) and the modulus (2^31) in the generator results in a pattern where triples of values fall on a series of equally spaced, parallel hyperplanes in three-dimensional space. This means the generator has a severely limited period in three dimensions, which is not suitable for many applications, especially those requiring three-dimensional random vectors, such as in simulations or cryptography.

Therefore, while RANDU was widely used at one time, it's now generally considered a textbook example of how not to design a pseudorandom number generator.

The Infamous RANDU Example R Code

# Load the necessary libraries library(tidyverse)

library(plotly)

# Define the number of terms n <- 1e

# Function to generate the LCG sequence generate_sequence <- function (a, m, x_0, n) { x_i <- numeric(n) x_i[ 1 ] <- x_ for (i in 2 :n) { x_i[i] <- (a * x_i[i - 1 ]) %% m } return(x_i) }

# Function to create a dataframe for plotting create_dataframe <- function (x_i) { nums <- x_i[ 2 :length(x_i)] / 2 ^ 31 nums_df <- tibble(x = nums[ 1 :(length(nums) - 2 )], y = nums[ 2 :(length(nums) - 1 )], z = nums[ 3 :length(nums)]) return(nums_df) }

# Function to create a 3D plot create_3d_plot <- function (nums_df, title) { plot_ly(nums_df, x = ~x, y = ~y, z = ~z, type = "scatter3d", mode = "markers",

# Desert Island x_0 <- 12345 a <- 16807 m <- 2 ^ 31 - 1 x_i <- generate_sequence(a, m, x_0, n) nums_df <- create_dataframe(x_i) create_3d_plot(nums_df, "Desert Island")