# Exercise Sheet 7 - Exercise 2c d <- 8 # window size entries <- rep(0,d*d) # initialize entries of window cm <- array(entries, dim=c(d,d)) # initialize the window n <- 1000 # set number n k <- 1000 # set number k lambda <- 1 # set lambda numvert <- rep(0,n) # counts the number of vertices having value 1 for (i in 1:k) { print(i) for (j in 1:n) { posx <- round(runif(1,0.5,d + 0.5)) # choose x-coordinate uniformly posy <- round(runif(1,0.5,d + 0.5)) # choose y-coordinate uniformly u <- runif(1) # uniform random number u # now check the values of the neighboring points and update if u < lambda/(lambda+1) and no neighbouring point equals 1 if ((cm[max(posx-1,1),posy] == 0) & (cm[min(posx+1,d),posy] == 0) & (cm[posx,max(posy-1,1)] == 0) & (cm[posx,min(posy+1,d)] == 0) & (cm[max(posx-1,1),max(posy-1,1)] == 0) & (cm[max(posx-1,1),min(posy+1,d)] == 0) & (cm[min(posx+1,d),max(posy-1,1)] == 0) & (cm[min(posx+1,d),min(posy+1,d)] == 0) & (u < lambda/(lambda+1))) { cm[posx,posy] <- 1 } else { cm[posx,posy] <- 0 } } numvert[i] <- sum(cm) # save number of vertices having value 1 after every n-th step } meanvalue <- mean(numvert) # calculates the mean number of vertices having value 1