set.seed()
, and we will have the psudorandom sequencean algorithm that produces a swquwnce od pseudorandom numbers based on the recurrence relation formula
# modular arithmatic
3 %% 4
# 3
3 %% 3
# 0
5 %% 4
# 0
# a === b (mod m)
# if that exists k belong to Z,
# a - b = Kn
# simulate numbers from 0,1,2,...,15
seed <- 10
new.random <- function(a=5, c=12, m=16){
out <- (a*seed + c) %% m
seed <<- out
return(out)
}
# random number generation
out.length <-20
variants <- rep(NA, out.length)
for(i in 1:out.length){
variants[i] <- new.reandom()
}
variants
<aside>
💡 <<-
allows you to addign a new global variable in a local environment
</aside>
# define a new function called my_unif()
# simulate n ind. draws from uni(0,1)
# draw 10,000 cases and plot a hist
my_unif() <- function(a, c, m, n){
out.length <-20
variants <- rep(NA, out.length)
for(i in 1:out.length){
variants[i] <- new.reandom(a = 1664545, c = 1013904223, m = 2^32)
}
return(variants)
}
seed <- 10
my_unif(n = 10)
X <- my_unif(n = 10000)
hist(X, prob = T)
X1 <- my_unif(n = 10000)
X2 <- my_unif(n = 10000)
Y <- X1+X2
hist(Y, prob = T)
# triangle distribution
# simulate rooling a fair 6-sided die
Z <- floor(6*my_unif(n = 10000))+1
# the probability of each number shows up
table(Z)/length(Z)
my_dics_unif <- function(n = 1){
out.length <- n
variants <- rep(Na, out.length)
}