帮我看看这是什么语言的写法# How many times should we run this experiment?iters = 1000results = rep(0,iters)for(i in 1:iters) {# A random permutation:boxes = sample(1:100,100)# Labels for our prisonersprisoners = 1:100# Track how many "wi

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/06 23:48:51
帮我看看这是什么语言的写法# How many times should we run this experiment?iters = 1000results = rep(0,iters)for(i in 1:iters) {# A random permutation:boxes = sample(1:100,100)# Labels for our prisonersprisoners = 1:100# Track how many

帮我看看这是什么语言的写法# How many times should we run this experiment?iters = 1000results = rep(0,iters)for(i in 1:iters) {# A random permutation:boxes = sample(1:100,100)# Labels for our prisonersprisoners = 1:100# Track how many "wi
帮我看看这是什么语言的写法
# How many times should we run this experiment?
iters = 1000
results = rep(0,iters)
for(i in 1:iters) {
# A random permutation:
boxes = sample(1:100,100)
# Labels for our prisoners
prisoners = 1:100
# Track how many "winners" we have
foundIt = 0
# Main loop over the prisoners
for(prisoner in prisoners) {
# Track the prisoners path
path = c(prisoner)
tries = 1
# Look first in the box that matches your own number
inBox = boxes[prisoner]
while(tries < 50) { path = c(path,inBox) if(inBox == prisoner) { #cat("Prisoner",prisoner,"found her number on try",tries,"\n") foundIt = foundIt + 1 break; } else { # Follow that number to the next box inBox = boxes[inBox] } tries = tries+1 } # cat("Prisoner",prisoner,"took path",paste(path,collapse=" -> "),"\n")
}
# How many prisoners found their numbers?
cat("A total of",foundIt,"prisoners found their numbers.\n")
flush.console()
results[i] = foundIt
}
hist(results,breaks=100,col="blue")

帮我看看这是什么语言的写法# How many times should we run this experiment?iters = 1000results = rep(0,iters)for(i in 1:iters) {# A random permutation:boxes = sample(1:100,100)# Labels for our prisonersprisoners = 1:100# Track how many "wi
java