Re: [R] Loop Issue

2014-05-23 Thread arun
Hi, You may also try: fun1 <- function(n, repl, val1, val2) {     mat1 <- suppressWarnings(replicate(repl, log(runif(n, val1, val2     mat1[!is.na(mat1)][seq(n)] } #Jim's function fun2 <- function(init, final, val1, val2) {     i <- init     while (i < final) {     u <- runif(1, val1, val2

Re: [R] Loop Issue

2014-05-23 Thread Jim Lemon
On Thu, 22 May 2014 09:11:43 PM Ricardo Rocha wrote: > Hi everybody. > > Consider the following exampling code: > > x=numeric() > for(i in 1:10){ > u=runif(1,-1,2) > x[i]=log(u) > } > This code, in each interation, generates a random value in the (-1,2) > interval and then calculates

Re: [R] Loop Issue

2014-05-22 Thread Yvan Richard
Hi Ricardo Assuming you have a good reason to use such approach (what are you trying to do ultimately?), you can just increment your counter when you get a good value, i.e.: x <- numeric() n <- 0 while (n < 10) { u <- log(runif(1, -1, 2)) if (is.finite(u)) { n <- n+1 x[n] <

[R] Loop Issue

2014-05-22 Thread Ricardo Rocha
Hi everybody. Consider the following exampling code: x=numeric() for(i in 1:10){ u=runif(1,-1,2) x[i]=log(u) } This code, in each interation, generates a random value in the (-1,2) interval and then calculates the log of the value. When the generated value is less than 0 the log pr