Skip to content
Prev 132829 / 398502 Next

Conditionally incrementing a loop counter: Take 2

Dear Mike,

You could use a repeat loop and manage the index yourself:

i <- 0
repeat{
	x <- runif(1)
	if (x < .1){
	   i <- i + 1
	   cat("x = ", x, "\n")
	   }
    if (i == 10) break
    }

But if your example problem reflects your actual application, why not just
generate uniform random numbers on the interval (0, .1)?

I hope this helps,
 John

--------------------------------
John Fox, Professor
Department of Sociology
McMaster University
Hamilton, Ontario, Canada L8S 4M4
905-525-9140x23604
http://socserv.mcmaster.ca/jfox