【发布时间】:2020-07-06 02:30:41
【问题描述】:
我试图在 while 循环中包含两个条件:
- 第一个条件是模拟次数必须至少为 50
- 第二个条件是 std(estimator) 必须小于 0.01.
有人可以帮助我吗?
set.seed(1)
MinSimNumber=50
Ncount=0
n=0
N=rep(0,10000)
sqrt(var(N))
while(Ncount<MinSimNumber){
u=runif(40,0,1) #Generating a big enough vector
if(u[n+1]>u[n+2]){ #if the preceding uniform value is bigger than execute the loop
while(u[n+1]>u[n+2]){ #while the preceding uniform value is bigger than the following than do n=n+1
n=n+1
}
n=n+2 #when the loop ends we do n=n+2 to add the first two not counted values
}
if((u[n+1]<=u[n+2])&(n==0)) {
n=2
}
Ncount=Ncount+1
N[Ncount]=n
n=0
}
这是我的完整代码,这个模拟近似于 exp(1)
【问题讨论】:
-
嗨,欢迎来到 SO!如果您编辑您的问题,这将极大地促进我们为您提供帮助的能力,以便提供[最小可重复示例]。(stackoverflow.com/help/minimal-reproducible-example)
标签: r while-loop conditional-statements