0

i have an assignment : enter image description here to solve this I wrote this code:

a1q1 <- function(n, j, k, m, seed){ 
  x_0=seed
  set_1=c(x_0)
  for (l in 1:k) {
    set_1[l+1]=(1103515245*set_1[l]+12345)%%2^32
    
  }
  for (f in 1:n) {
    set_1[k+1+f]=(set_1[f-j+k+1]+set_1[f+1])%%m
    
  }
return(set_1[k+2:length(set_1)])
}

a1q1(sample(200:300,1),36,99,2^30,202)

sad returns me NA values and I don't understand why. Would be happy for help , was trying to figure out the problem for hours.

This is the output:

enter image description here

tried to write my code but seems to be producing only NA.

i also have some test to check my code :

# Q1
# Checks that the code for "a1q1" produces a numeric vector 
# of length n
n <- sample(200:300,1)
j <- 36
k <- 99
m <- 2^(30)
seed <- 2022
X <- a1q1(n,j,k,m,seed)
if((length(X) != n) | (!is.numeric(X))){
  stop("a1q2 does not produce a vector of length n of integers")
}

gives back"does not produce a vector of length n of integers"

  • The `return` statement is inside the `for` loop, the function returns after computing just one number. Try putting the `return` statement as the last function instruction. – Rui Barradas Mar 27 '22 at 14:34
  • in your return statement, you need to put a bracket around k + 2, currently it is creating the sequence `2:length(set_1)`, then adding `k` to it, which leads to incorrect length of the returned result. It should be `return(set_1[(k+2) : length(set_1)])` – AdroMine Mar 27 '22 at 15:32

0 Answers0