
# file "Rrng" -- a simple linear congruential pseudorandom number generator

# choices of the parameter values -- may be changed
m = 2^(32)
a = 69069
b = 23606797

# Create seed value, either manually:
latestval = 12345  # seed value -- may be changed
# ... or using current milliseconds of computer time.
options(digits.sec=3)
latestval = as.integer( as.numeric( format(Sys.time(), "%OS") ) * 1000 )

# Define a "remainder" function.
remainder = function(n,m) {
    return( n - m * floor(n/m) )
}

# Define a "nextrand" function, to create the next pseudorandom number.
nextrand = function() {
    latestval <<- remainder(a*latestval+b, m)   # (global assignment)
    return(latestval / m)
}

# Print out ten successive pseudorandom numbers.
for (i in 1:10)
  print( nextrand() )
cat("\n")  # leave a blank line

# Create 1000 pseudorandom numbers; compute their mean and second moment.
x = rep(0,1000)
for (i in 1:1000)
  x[i] = nextrand()
print( mean(x) )
print( mean(x^2) )

