"Do Until" Loops in R

I am trying to code the following game in R:

  • Roll a dice until you observe a 4 followed by a 6
  • Count how many times it took you to observe a 4 followed by a 6
  • Repeat these first two steps 100 times
  • Calculate the average number of times it took to observe a 4 followed by a 6

I tried to manually simulate this as follows - I first used the "runif" command in R to "roll a dice" a large number of times, hoping that you will eventually see a 4 followed by a 6 (I don't know how to code this using "do until loops"). I repeated this 100 times and put all these rolls into a data frame:

roll_1 = floor(runif(100, min=1, max=6))

roll_2 = floor(runif(100, min=1, max=6))

roll_3 = floor(runif(100, min=1, max=6))

roll_4 = floor(runif(100, min=1, max=6))

roll_5 = floor(runif(100, min=1, max=6))

#etc 

roll_100 = floor(runif(100, min=1, max=6))

all_rolls = data.frame(roll_1, roll_2, roll_3, roll_4, roll_5, roll_100)

This looks as follows:

head(all_rolls)
  roll_1 roll_2 roll_3 roll_4 roll_5 roll_100
1      4      2      5      3      1        4
2      3      2      4      4      1        2
3      1      3      1      4      2        1
4      3      2      1      4      4        3
5      4      1      2      2      5        5
6      2      3      3      5      3        1

I then exported this data frame into Microsoft Excel and manually inspected each column and counted the row number at which a 6 appears when preceded by a 4. I then averaged this number for all columns and calculated the average number of times you need to roll a dice before you observe a 4 followed by a 6. This took some time to do, but it worked.

I am looking for a quicker way to do this. Does anyone know if "do until" loops can be used in R to accelerate this "game"?

Thanks

"Do Until" -> "while" loop (Edit: Refer to @Hong's post. I've never used repeat to be honest, and can't see a situation where I can't use while, but while seems more like "Do If")

Here's one way to do it, if I understand the problem correctly.

simulate_raw <- function(first_side, second_side)
{
	first_roll <- sample(1:6, 1)
	second_roll <- sample(1:6, 1)

	no_of_rolls <- 2

	while ((first_roll != first_side) || (second_roll != second_side))
	{
		first_roll <- second_roll
		second_roll <- sample(1:6, 1)

		no_of_rolls <- no_of_rolls + 1
	}

	return(no_of_rolls)
}

first_die <- 4
second_die <- 6
num_repeats <- 100

simulations <- replicate(num_repeats, simulate_raw(first_die, second_die))

mean(simulations)

Hope this helps.

1 Like

Answering the title of the question rather than the body: a 'do ... until' loop in R is of the form

repeat {
  # code
  if(stop_condition_is_true)
    break
}

If you don't include the break somewhere inside the repeat block, it will loop indefinitely.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.