Q:

Generating sequence of random numbers

I need to create a function of five variables,

a (multiplier)
n (sample size)
c (increment with default 0)
m (modulus)
x0 (Initial seed value)

I need to generate a sequence of random numbers with the equation

xi = (a*xi-1 + c) (mod m), i = 1, 2, ..., n

As in the vector x = (x1, ..., xn).
My attempt:

my.unif1 <- function(n, a,c = 0, m, x = x[0]) {
  while(n > 0) {
    x[n] <- (a*x[n-1]+c)%%m
  }
}

A:

It sounds like you want to learn more about Linear Congruential Generators. Here's a resource that will probably help you solve your code problem: https://qualityandinnovation.com/2015/03/03/a-linear-congruential-generator-lcg-in-r/
lcg <- function(a,c,m,run.length,seed) {
    x <- rep(0,run.length)
    x[1] <- seed
    for (i in 1:(run.length-1)) {
       x[i+1] <- (a * x[i] + c) %% m
    }
    U <- x/m # scale all of the x's to
         # produce uniformly distributed
         # random numbers between [0,1)
    return(list(x=x,U=U))
}

> z <- lcg(6,7,23,20,5)
> z
$x
[1] 5 14 22 1 13 16 11 4 8 9 15 5 14 22 1 13 16 11
[19] 4 8

$U
[1] 0.21739130 0.60869565 0.95652174 0.04347826 0.56521739
[6] 0.69565217 0.47826087 0.17391304 0.34782609 0.39130435
[11] 0.65217391 0.21739130 0.60869565 0.95652174 0.04347826
[16] 0.56521739 0.69565217 0.47826087 0.17391304 0.34782609

