This week, one of the topics in random number generation. Using the software / spreadsheet of your choice, build one of the random number generators discussed in Banks DES. Post your code to the discussion board.

Linear Congruental Method

Linear congruental method is one of the techniques used for generating random numbers. This is the formula employed by this method:

\(X_{n} = (aX_{n-1} + c)\) \(mod\) \(m\)

where

\(m\) is the modulus

\(a\) is a constant multiplier

\(c\) is the increment

and

\(X_0\) is the seed

# function to calculate Linear Congruental Method
LCG <- function (s, n, a,c, m){
  seq <- c(s,2:n)
  for(i in c(2:n)){
    seq[i] <- (a*seq[i-1] + c) %% m
  }
  return(seq)
    
}

# run function with low modulus value
rn1 <- LCG(1,100,1,1,8)
sum1 <- summary(rn1)
sum1
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    0.00    1.75    3.00    3.46    5.00    7.00
plot(rn1, main="Plot #2 - LCG (a= 21, c=1 and m=8)")
rn1
##   [1] 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3
##  [36] 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6
##  [71] 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4
abline(h=sum1[1])
abline(h=summary(rn1)[3])
abline(h=summary(rn1)[6])

# run function with very high modulus value
rn2 <- LCG(1,100,2,1,6547694)
rn2
##   [1]       1       3       7      15      31      63     127     255
##   [9]     511    1023    2047    4095    8191   16383   32767   65535
##  [17]  131071  262143  524287 1048575 2097151 4194303 1840913 3681827
##  [25]  815961 1631923 3263847 6527695 6507697 6467701 6387709 6227725
##  [33] 5907757 5267821 3987949 1428205 2856411 5712823 4877953 3208213
##  [41] 6416427 6285161 6022629 5497565 4447437 2347181 4694363 2841033
##  [49] 5682067 4816441 3085189 6170379 5793065 5038437 3529181  510669
##  [57] 1021339 2042679 4085359 1623025 3246051 6492103 6436513 6325333
##  [65] 6102973 5658253 4768813 2989933 5979867 5412041 4276389 2005085
##  [73] 4010171 1472649 2945299 5890599 5233505 3919317 1290941 2581883
##  [81] 5163767 3779841 1011989 2023979 4047959 1548225 3096451 6192903
##  [89] 5838113 5128533 3709373  871053 1742107 3484215  420737  841475
##  [97] 1682951 3365903  184113  368227
sum2 <- summary(rn2)
sum2
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##       1  863658 3152332 3084780 5242084 6527695
plot(rn2, main="Plot #2 - LCG (a= 1, c=1 and m=6547694)")
abline(h=sum2[1])
abline(h=summary(rn2)[3])
abline(h=summary(rn2)[6])

# run function with high modulus and high multiplier values
rn3 <- LCG(1,100,14560,1,6547694)
rn3
##   [1]       1   14561 2481953  512495 4103735 2673851 5229731 1749835
##   [9]  520247 5662057 4082461  666029  247427 1305421 5521773 4427949
##  [17] 2342317 3745169  465009  215445  533775 6198917 2817425  405091
##  [25] 5200361 6270439 3094399 6314721 6166307 5997487 3363537 2895295
##  [33] 1441229 5482665 4664847  942459 4784111 2287389 2812157 2275339
##  [41] 4151895 3280193  730045 2547839 3849331 4546415 5163755 3650293
##  [49]  633883 3635635 3287305 6065355 2819823 2581501 2891001 4397529
##  [57] 4670309 1896851 6524963 2969035 1273813 3647873 4684847 4043923
##  [65] 2654433 4054493 5956671 4922731 3904837  799719 2108709  665875
##  [73] 4552881 1093305 1076687 1383285 6470551 3001289 6005779 6236565
##  [81]  966009  644329 5132433 5940553 5961635 5173937 1303251  117349
##  [89] 6201001  421995 2510229 6254027 6400357 2416913 2945725 2360301
##  [97] 3684449  320499 4507313 5488013
sum3 <- summary(rn3)
sum3
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##       1 1363819 3187296 3274970 4975156 6524963
plot(rn3, main="Plot #3 - LCG -(a= 14560, c=1 and m=6547694)")
abline(h=sum3[1])
abline(h=summary(rn3)[3])
abline(h=summary(rn3)[6])

What challenges did I have?

As I selected different values from a, c, m and \(X_0\), it affected the output differently for each set. The challenge I faced was selecting the right parameter values.

The plot 1 with low \(a\) and \(m\) values, shows that random number sequences repeat after generating 8 at a time. That’s a very low sequence period.

The plot 2 with low \(a\) and high \(m\) starts with very low scale numbers then goes higher. This a bit of strange patter that I did not expect. The sequence of repitions is low.

The plot 3 with high \(a\) and \(m\) gererates better random variable sequences of all three sets.