# recursive method - only returns one random number
recursive.randGen = function(seed, a, c, m, n = 10) {
if (n == 1) {
return(seed)
} else {
return((a * recursive.randGen(seed, a, c, m, n - 1) + c) %% m)
}
}
for (i in 1:10) {
print(recursive.randGen(1,2,3,499,i))
}
## [1] 1
## [1] 5
## [1] 13
## [1] 29
## [1] 61
## [1] 125
## [1] 253
## [1] 10
## [1] 23
## [1] 49
# loop method - prints all n random numbers
loop.randGen = function(seed, a, c, m, n = 10) {
x = seed
print(x)
for (i in 2:n) {
x = (a * x + c) %% m
print(x)
}
}
loop.randGen(1,2,3,499,10)
## [1] 1
## [1] 5
## [1] 13
## [1] 29
## [1] 61
## [1] 125
## [1] 253
## [1] 10
## [1] 23
## [1] 49