stats_noob
stats_noob

Reputation: 5897

Generating Random Numbers Until Some Condition Is Met

I am working with the R programming language. I am trying to learn more about WHILE LOOPS and thought of the following problem to implement them in:

I tried to do this like this:

num_1_i = num_2_i = num_3_i = 0

list_results <- list()

for (i in 1:100)

{

while (num_1_i + num_2_i + num_3_i < 150) {


turn_i = i
num_1_i = runif(1,0,100)
num_2_i = runif(1,0,100)
num_3_i = runif(1,0,100)
break
}

inter_results_i <- data.frame(turn_i, num_1_i, num_2_i, num_3_i)

list_results[[i]] <- inter_results_i
   
}

This is producing the same numbers every time:

[[98]]
  turn_i  num_1_i num_2_i  num_3_i
1      1 87.67886 32.7703 52.72885

[[99]]
  turn_i  num_1_i num_2_i  num_3_i
1      1 87.67886 32.7703 52.72885

[[100]]
  turn_i  num_1_i num_2_i  num_3_i

Can someone please show me what I am doing wrong and how to fix this?

Thanks!

Note: Alternate Method I tried - still doesn't work:

list_results <- list()

for (i in 100) {
  repeat {
   

turn_i = i
num_1_i = runif(1,0,100)
num_2_i = runif(1,0,100)
num_3_i = runif(1,0,100)

    if(num_1_i + num_2_i + num_3_i < 150) break
  }
  results_tmp <- data.frame(turn = i, num_1 = num_1_i, num_2 = num_2_i, num_3 = num_3_i)
  
  list_results[[i]] <- results_tmp
}

Upvotes: 1

Views: 875

Answers (3)

jkuo_ut
jkuo_ut

Reputation: 131

Your first attempt using while is on track, just need one slight modification: putting the line num_1_i = num_2_i = num_3_i = 0 inside the for-loop, outside the while-loop

for (i in 1:100){
  num_1_i = num_2_i = num_3_i = 0
  
  while(num_1_i + num_2_i + num_3_i < 150){
    num_1_i = runif(1,0,100)
    num_2_i = runif(1,0,100)
    num_3_i = runif(1,0,100)
  }
  
  inter_results_i <- data.frame(i, num_1_i, num_2_i, num_3_i)
  list_results[[i]] <- inter_results_i
}

What's happening is that during i = 1, you've generated 3 numbers that satisfy the condition, but they were never reset to zero. So every subsequent iteration of for it is immediately skipping the while.

Upvotes: 3

viggnah
viggnah

Reputation: 1879

It is because after finding the first combination that exceeds 150 you don't reset the numbers back to 0. So it never enters the while loop again and the same numbers are stored 100 times.

Simply move the first line of setting numbers to 0 into the for loop:

list_results <- list()

for (i in 1:100)

{
num_1_i = num_2_i = num_3_i = 0

while (num_1_i + num_2_i + num_3_i < 150) {

turn_i = i
num_1_i = runif(1,0,100)
num_2_i = runif(1,0,100)
num_3_i = runif(1,0,100)
break
}

inter_results_i <- data.frame(turn_i, num_1_i, num_2_i, num_3_i)

list_results[[i]] <- inter_results_i
   
}

Upvotes: 1

MojSkin
MojSkin

Reputation: 143

Lets check your code:

while (num_1_i + num_2_i + num_3_i < 150) {
  turn_i = i
  num_1_i = runif(1,0,100)
  num_2_i = runif(1,0,100)
  num_3_i = runif(1,0,100)
  break >>>> LOOK AT HERE! YOUR LOOP WILL WORK ONLY ONE TIME AND BREAKS HERE
}

And there is some difference between WHILE and REPEAT loops:

In WHILE loops the criteria(s) is/are checked in the begining of the loop, and if it returns TRUE, code block will run, in this situation maybe your code is not working fine! But in REPEAT loops, code block will run at least one time! look at here:

repeat{
  num_1_i = runif(1,0,100)
  num_2_i = runif(1,0,100)
  num_3_i = runif(1,0,100)
  if(num_1_i + num_2_i + num_3_i < 150){
    break
  }
}

In my opinion, you should use REPEAT in this case.

Good luck!

Upvotes: 2

Related Questions