使用WHILE的无限循环,即使在R中满足条件
我试图学习如何实现控制结构,如FOR和while循环 我创建了一个函数来模拟著名的巴西彩票中的下注。 在乐透中,玩家从1:60的向量中下注6个唯一的整数(称为“你的赌注”)。 该函数从1到60个宇宙(“结果”)中采样6个值,并测试结果中有多少值与您的赌注匹配,打印出: 你的赌注 结果 总分(共6分) 关于赌注结果的三种可能的评论之一 代码如下:使用WHILE的无限循环,即使在R中满足条件,r,while-loop,R,While Loop,我试图学习如何实现控制结构,如FOR和while循环 我创建了一个函数来模拟著名的巴西彩票中的下注。 在乐透中,玩家从1:60的向量中下注6个唯一的整数(称为“你的赌注”)。 该函数从1到60个宇宙(“结果”)中采样6个值,并测试结果中有多少值与您的赌注匹配,打印出: 你的赌注 结果 总分(共6分) 关于赌注结果的三种可能的评论之一 代码如下: ``` LOTTO<-function(your_bet=sample(1:60, size=6, replace=FALSE)){ r
```
LOTTO<-function(your_bet=sample(1:60, size=6, replace=FALSE)){
result<-sample(1:60, size=6, replace=FALSE)
logical_vector<-(your_bet %in% result)
total_points<-sum(as.integer(logical_vector))
print(paste(c("Your bet:", as.character(your_bet))), collapse="")
print(paste(c("Result", as.character(result))), collapse="")
print(paste(c("Total points", as.character(total_points))), collapse="")
if (total_points==6)
print("You are a millonaire")
else if (total_points==5)
print("5 points, you are rich!")
else print("good luck next time")
}
```
```
LOTTO我还没有深入研究这个函数,但它在任何时候都不应该中断的唯一原因是当目标点>n_样本时(这里是6)
这种情况下的问题非常明显,而且很容易解决。将target_points
减少到6
以下,或添加n个样本
(6 atm.),并使其大于target_points
。然而,我怀疑主要的问题在于递归函数<当涉及到递归时,code>R
是相当严格的,例如,如果一个人试图做一个简单的递归
i <- 0
f <- function(){
i <<- i + 1
f()
}
f()
i
# 896 on my pc
函数调用与以前一样,但现在还返回尝试次数和从试验中获得的结果,如下所示
LOTTO(target_points = 6, stubborn_until_x_points = TRUE)
You're a millionaire!
#Output:
result number_of_tries
"You're a millionaire!\n" "8297820"
删除递归(包括while循环中的函数体)、将-Inf分配给初始的total_点以及添加break语句都非常有用。
通过@Oliver添加答案,得到了我想要的结果:
LOTTO<-function(your_bet=sample(1:60, size=6, replace=FALSE), stubborn_until_x_points=FALSE,
target_points=0){
total_points<--Inf
while(total_points < target_points){
result<-sample(1:60, size=6, replace=FALSE)
logical_vector<-(your_bet %in% result)
total_points<-sum(as.integer(logical_vector))
print(paste(c("Your bet:", as.character(your_bet))), collapse="")
print(paste(c("Result", as.character(result))), collapse="")
print(paste(c("Total points", as.character(total_points))), collapse="")
if (total_points==6)
print("You are a millonaire")
else if (total_points==5)
print("5 points, you are rich!")
else print("good luck next time")
if (isFALSE(stubborn_until_x_points==TRUE))
break
}
LOTTOIt看起来您没有更新loopA友好评论中的总积分。这个函数有一些不必要的递归。将所有代码封装在while
循环中会更有效(而且C-stack-safe)。简单地从定义total_points开始,我该怎么做?你能给我看看吗@如果目标点大于60,那么它也将处于无限循环中,不是吗?你查过了吗?
LOTTO(target_points = 6, stubborn_until_x_points = TRUE)
You're a millionaire!
#Output:
result number_of_tries
"You're a millionaire!\n" "8297820"
LOTTO<-function(your_bet=sample(1:60, size=6, replace=FALSE), stubborn_until_x_points=FALSE,
target_points=0){
total_points<--Inf
while(total_points < target_points){
result<-sample(1:60, size=6, replace=FALSE)
logical_vector<-(your_bet %in% result)
total_points<-sum(as.integer(logical_vector))
print(paste(c("Your bet:", as.character(your_bet))), collapse="")
print(paste(c("Result", as.character(result))), collapse="")
print(paste(c("Total points", as.character(total_points))), collapse="")
if (total_points==6)
print("You are a millonaire")
else if (total_points==5)
print("5 points, you are rich!")
else print("good luck next time")
if (isFALSE(stubborn_until_x_points==TRUE))
break
}