For 循环包括 "if" 和 "while" 运行 几个小时
For loop including "if" and "while" running for hours
我有一个 for 循环,包括为一个问题编写的“if”和“while”子句。它旨在在某些条件下进行 1000 次模拟。我认为这不是一个非常复杂的循环,但是已经 运行 将近 16 个小时没有显示结果或提示 error/warning (并且一直显示红色的小停止标志),自从我开始 运行 循环以来,我可以感觉到我的笔记本电脑的速度变慢了。
所以我想知道这是否真的会发生,或者我的代码或我的笔记本电脑是否有问题。任何帮助是极大的赞赏!!
请看下面的代码:
result.Vec <- NULL
for (trials in 1:1000) {
sum <- 0
sum2 <- 0
n <- 0
tmp1 <- sample(x=c(1, 2, 3, 4, 5, 6), size=1, replace=T, prob=c(1/6, 1/6, 1/6, 1/6, 1/6, 1/6))
tmp2 <- sample(x=c(1, 2, 3, 4, 5, 6), size=1, replace=T, prob=c(1/6, 1/6, 1/6, 1/6, 1/6, 1/6))
sum <- tmp1 + tmp2
if (sum == 7 || sum == 11) {
n <- 1
} else if (sum == 2 || sum == 3 || sum == 12) {
n <- 0
} else {
while (sum2 != sum || sum2 != 7) {
tmp1 <- sample(x=c(1, 2, 3, 4, 5, 6), size=1, replace=T, prob=c(1/6, 1/6, 1/6, 1/6, 1/6, 1/6))
tmp2 <- sample(x=c(1, 2, 3, 4, 5, 6), size=1, replace=T, prob=c(1/6, 1/6, 1/6, 1/6, 1/6, 1/6))
sum2 <- tmp1 + tmp2
if (sum2 == sum) {
n <- 1
} else if (sum2 == 7) {
n <- 0
}
}
}
result.Vec <- c(result.Vec, n)
}
我循环的问题请看下面作为参考(不求解决这个问题):
掷骰子游戏的玩法如下。玩家掷两个骰子,如果总和为七或十一,则她获胜。如果总和是二、三或十二,那么她就输了。如果总和是其他数字,则她继续投掷,直到她再次投出那个数字(在这种情况下她赢了)或者她投出一个 7(在这种情况下她输了)。根据1000次模拟计算玩家获胜的概率。
while 循环卡住:
while (sum2 != sum || sum2 != 7)
如果 sum != 7(所以对于几乎任何可能的值)这将始终评估为 TRUE。
首先,为了加快代码速度,您应该摆脱外部 for 循环。而是使用模拟一场比赛的功能,然后使用 replicate
到 运行 比赛 n
次。
其次,使用break
退出while循环。另外,请注意将变量命名为 sum
是不明智的,因为已经有一个名为 sum
.
的 R 函数
这是我的代码版本:
simulate_game <- function(){
tmp1 <- sample(x = 1:6, size = 1)
tmp2 <- sample(x = 1:6, size = 1)
mysum <- tmp1 + tmp2
if (mysum %in% c(7, 11)) {
n <- 1
} else if (mysum %in% c(2, 3, 12)) {
n <- 0
} else {
mysum2 <- 0
while (! mysum2 %in% c(mysum, 7)) {
tmp1 <- sample(x = 1:6, size = 1)
tmp2 <- sample(x = 1:6, size = 1)
mysum2 <- tmp1 + tmp2
if (mysum2 == mysum) {
n <- 1; break()
} else if (mysum2 == 7) {
n <- 0; break()
}
}
}
return(n)
}
现在您可以使用以下方法模拟 1000 运行s:
set.seed(1)
table(replicate(1000, simulate_game()))
0 1
530 470
请注意,我使用 set.seed
设置了随机种子。这使您的结果可重现。
正如我在评论中提到的,问题出在 while 循环中的条件上。它应该是 AND 而不是 OR,否则 while 循环将始终失败。我已经对此进行了测试并且有效:
result.Vec <- NULL
for (trials in 1:1000) {
print(trials)
sum <- 0
sum2 <- 0
n <- 0
tmp1 <- sample(x=c(1, 2, 3, 4, 5, 6), size=1, replace=T, prob=c(1/6, 1/6, 1/6, 1/6, 1/6, 1/6))
tmp2 <- sample(x=c(1, 2, 3, 4, 5, 6), size=1, replace=T, prob=c(1/6, 1/6, 1/6, 1/6, 1/6, 1/6))
sum <- tmp1 + tmp2
if (sum == 7 | sum == 11) {
n <- 1
} else if (sum == 2 | sum == 3 | sum == 12) {
n <- 0
} else {
while (sum2 != sum & sum2 != 7) {
tmp1 <- sample(x=c(1, 2, 3, 4, 5, 6), size=1, replace=T, prob=c(1/6, 1/6, 1/6, 1/6, 1/6, 1/6))
tmp2 <- sample(x=c(1, 2, 3, 4, 5, 6), size=1, replace=T, prob=c(1/6, 1/6, 1/6, 1/6, 1/6, 1/6))
sum2 <- tmp1 + tmp2
if (sum2 == sum) {
n <- 1
} else if (sum2 == 7) {
n <- 0
}
}
}
result.Vec <- c(result.Vec, n)
}
我有一个 for 循环,包括为一个问题编写的“if”和“while”子句。它旨在在某些条件下进行 1000 次模拟。我认为这不是一个非常复杂的循环,但是已经 运行 将近 16 个小时没有显示结果或提示 error/warning (并且一直显示红色的小停止标志),自从我开始 运行 循环以来,我可以感觉到我的笔记本电脑的速度变慢了。
所以我想知道这是否真的会发生,或者我的代码或我的笔记本电脑是否有问题。任何帮助是极大的赞赏!!
请看下面的代码:
result.Vec <- NULL
for (trials in 1:1000) {
sum <- 0
sum2 <- 0
n <- 0
tmp1 <- sample(x=c(1, 2, 3, 4, 5, 6), size=1, replace=T, prob=c(1/6, 1/6, 1/6, 1/6, 1/6, 1/6))
tmp2 <- sample(x=c(1, 2, 3, 4, 5, 6), size=1, replace=T, prob=c(1/6, 1/6, 1/6, 1/6, 1/6, 1/6))
sum <- tmp1 + tmp2
if (sum == 7 || sum == 11) {
n <- 1
} else if (sum == 2 || sum == 3 || sum == 12) {
n <- 0
} else {
while (sum2 != sum || sum2 != 7) {
tmp1 <- sample(x=c(1, 2, 3, 4, 5, 6), size=1, replace=T, prob=c(1/6, 1/6, 1/6, 1/6, 1/6, 1/6))
tmp2 <- sample(x=c(1, 2, 3, 4, 5, 6), size=1, replace=T, prob=c(1/6, 1/6, 1/6, 1/6, 1/6, 1/6))
sum2 <- tmp1 + tmp2
if (sum2 == sum) {
n <- 1
} else if (sum2 == 7) {
n <- 0
}
}
}
result.Vec <- c(result.Vec, n)
}
我循环的问题请看下面作为参考(不求解决这个问题): 掷骰子游戏的玩法如下。玩家掷两个骰子,如果总和为七或十一,则她获胜。如果总和是二、三或十二,那么她就输了。如果总和是其他数字,则她继续投掷,直到她再次投出那个数字(在这种情况下她赢了)或者她投出一个 7(在这种情况下她输了)。根据1000次模拟计算玩家获胜的概率。
while 循环卡住:
while (sum2 != sum || sum2 != 7)
如果 sum != 7(所以对于几乎任何可能的值)这将始终评估为 TRUE。
首先,为了加快代码速度,您应该摆脱外部 for 循环。而是使用模拟一场比赛的功能,然后使用 replicate
到 运行 比赛 n
次。
其次,使用break
退出while循环。另外,请注意将变量命名为 sum
是不明智的,因为已经有一个名为 sum
.
这是我的代码版本:
simulate_game <- function(){
tmp1 <- sample(x = 1:6, size = 1)
tmp2 <- sample(x = 1:6, size = 1)
mysum <- tmp1 + tmp2
if (mysum %in% c(7, 11)) {
n <- 1
} else if (mysum %in% c(2, 3, 12)) {
n <- 0
} else {
mysum2 <- 0
while (! mysum2 %in% c(mysum, 7)) {
tmp1 <- sample(x = 1:6, size = 1)
tmp2 <- sample(x = 1:6, size = 1)
mysum2 <- tmp1 + tmp2
if (mysum2 == mysum) {
n <- 1; break()
} else if (mysum2 == 7) {
n <- 0; break()
}
}
}
return(n)
}
现在您可以使用以下方法模拟 1000 运行s:
set.seed(1)
table(replicate(1000, simulate_game()))
0 1
530 470
请注意,我使用 set.seed
设置了随机种子。这使您的结果可重现。
正如我在评论中提到的,问题出在 while 循环中的条件上。它应该是 AND 而不是 OR,否则 while 循环将始终失败。我已经对此进行了测试并且有效:
result.Vec <- NULL
for (trials in 1:1000) {
print(trials)
sum <- 0
sum2 <- 0
n <- 0
tmp1 <- sample(x=c(1, 2, 3, 4, 5, 6), size=1, replace=T, prob=c(1/6, 1/6, 1/6, 1/6, 1/6, 1/6))
tmp2 <- sample(x=c(1, 2, 3, 4, 5, 6), size=1, replace=T, prob=c(1/6, 1/6, 1/6, 1/6, 1/6, 1/6))
sum <- tmp1 + tmp2
if (sum == 7 | sum == 11) {
n <- 1
} else if (sum == 2 | sum == 3 | sum == 12) {
n <- 0
} else {
while (sum2 != sum & sum2 != 7) {
tmp1 <- sample(x=c(1, 2, 3, 4, 5, 6), size=1, replace=T, prob=c(1/6, 1/6, 1/6, 1/6, 1/6, 1/6))
tmp2 <- sample(x=c(1, 2, 3, 4, 5, 6), size=1, replace=T, prob=c(1/6, 1/6, 1/6, 1/6, 1/6, 1/6))
sum2 <- tmp1 + tmp2
if (sum2 == sum) {
n <- 1
} else if (sum2 == 7) {
n <- 0
}
}
}
result.Vec <- c(result.Vec, n)
}