$total = 0; $count = 0;
while ($count < 100) {
$number = mt_rand(0, 100); // get a random number from 0 to 100
$total += $number; $count++;
}
$average = $total / $count;
echo("<p>The total of $count random numbers is " . number_format($total) . " and the average is: " . number_format($average, 2) . "</p>>\n");
The total of 100 random numbers is 4,761 and the average is: 47.61
$rolls = 1;
while (mt_rand(1,6) != 6) { // get a random number from 1 to 6
$rolls++;
}
echo("<p>Number of times to roll a six: $rolls.</p>\n");
Number of times to roll a six: 5.
$total = 0;
$count = 0;
$max = -INF;
while ($count < 10000) {
$rolls = 1;
while (mt_rand(1, 6) != 6) { // get a random number from 1 to 6
$rolls++;
}
$total += $rolls;
$count++;
$max = max($rolls, $max); // get the value of the larger argument
$average = $total / $count;
}
echo("<p>Average: $average Max: $max;</p>\n");
Average: 5.9984 Max: 47;