Using the do/while loop

A do/while loop that counts dice rolls until a six is rolled

$rolls = 1;
do {
  $rolls++;
} while (mt_rand(1,6) != 6); // get a random number from 1 to 6
echo("<p>Number of times to roll a six: $rolls.</p>\n");

Number of times to roll a six: 18.

A do-while loop to find the max and min of 10 random values

$max = -INF;
$min = INF;
$count = 0;
do {
  $number = mt_rand(0, 100);
  $max = max($max, $number);
  $min = min($min, $number); $count++;
}
while ($count < 10);
echo("<p>Max: $max Min: $min</p>\n");

Max: 99 Min: 5

Back