# Code to view correlation in pseudorandom numbers generated by a LCG # David Read, California State University, Easy Bay, 3/30/09 library(rgl) # Note: This is a TERRIBLE LCG used only to show the correlation. N=26 Seed = 44 Mvalue = 26 Cvalue = 27 Dvalue = 3 v1 = numeric(N) v1[1] = Seed for (i in 2:N) {Next = (Cvalue*Seed + Dvalue)%%Mvalue v1[i] = Next Seed = Next } v2<-v1/Mvalue plot(v2[1:(N-1)], v2[2:N], main="Correlation of Pseudorandom Numbers from a Linear Congruential Generator") # Note: This LCG has passed, and still passes, many tests for "randomness" # and is suggested for use by the U.S. Bureau of Standards in 1964. N=100000 Seed = 762939453125 Mvalue = 2^47 Cvalue = (2^7)+1 Dvalue = 29741096258473 v1 = numeric(N) v1[1] = Seed for (i in 2:N) {Next = (Cvalue*Seed + Dvalue)%%Mvalue v1[i] = Next Seed = Next } v2<-v1/Mvalue # View correlation in 2-dimensions. X-axis is v[i-1], Y-axis is v[i]. # Expand this image to full screen to see the "structure." plot(v2[1:(N-1)], v2[2:N], main="Correlation of Pseudorandom Numbers from a Linear Congruential Generator") # View correlation in 3-dimensions. X-axis is v[i-2], Y-axis is v[i-1], # Z-axis is v[i]. # NOTE: You MUST install and load the R package rgl for this code to work. # The 3d image can be rotated by clicking and dragging. The mouse wheel zooms. # The "structure" can be seen by rotating the image until the empty planes can # be seen. Again, full screen is best. plot3d(v2[1:(N-2)], v2[2:(N-1)], v2[3:N], main="Correlation of Pseudorandom Numbers from a Linear Congruential Generator")