"Do Until" Loops in R

"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