Question: my.rand.generator is a function that produces an LCG function that takes in an integer n. my.rand.generator

my.rand.generator is a function that produces an LCG function that takes in an integer n.

my.rand.generator <- function(m,a,c,seed){ #this is the function i wrote LCG <- function(n){ if(n == -2) { return (list(m,a,c)) } if(n >0){ x <- rep(0,n) x[1] <- seed for(i in 1:(n-1)){ x[i+1] <- (a * x[i] + c) %% m } return(list(x)) } }

}

E.G of execution: my.rand <- my.rand.generator(128,15,7,13) my.rand(5)

1. Write a function that takes the LCG produced by my.rand.generator(a,c,m,seed) and calculates the periodicty (P) of the LCG as well as breaking it down into the loop length(L) and lead-in sequence length(S), where the loop length is the number of random numbers that are repeated if the LCG is allowed to cycle and the lead-in sequence length is the number of random numbers produced before it goes into the loop. Note that P= L+S. return the three values as a vector c(P,L,S)

eg. my.rand <- my.rand.generator(14,39,100,8)

periodicty(my.rand)

STORE THE LCG FUNCTION PRODUCED TO ANSWER THE SECOND PART

b. use the LCG function stored in my.rand to write a function my.runif(n,min=0,max=1,rand = my.rand) that behaves the same as runif() except that it uses your random number generator.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!