n
RIには、forループ時間を繰り返す次のサンプルモジュールがあります。
function(n){
#inputs - n - number of results required
#reserve n spaces for results
r_num_successes <- 1:n
#start looping n times
for(i in 1:n){
#set first uniform "random" deviate equal to 0.05 and number of successes to 0
current_unif <- 0.05
num_successes <- 0
#start while loop that updates current_unif - it runs as long as
#current_unif is less than 0.95, increments num_successes each loop
while(current_unif < 0.95){
#set current_unif to a uniform random deviate between the
#existing current_unif and 1
current_unif <- runif(1,current_unif)
num_successes <- num_successes + 1
}
#set the i-th element of the results vector to that final num_successes
#generated by the while loop
r_num_successes[i] <- num_successes
}
#output the mean of all the successes
return(mean(r_num_successes))
}
n
大きくなると、これはかなりゆっくりと粉砕し始めます。それを最適化する良い方法はありますか?