Well, here is a script which does exactly what you were wanting:
<?php
$numrolls = 10;
$numsides = 6;
for ($i=0;$i<$numrolls;$i++) {
$rolls[$i] = rand(1,$numsides);
}
for ($i=0;$i<count($rolls);$i++) {
$rollcounts = array_count_values($rolls);
}
$mostcommon = 0;
for ($i=0;$i<count($rollcounts);$i++) {
if ($rollcounts[$i] >= $mostcommon) {
$mostcommon = $i;
}
}
echo "The most common roll was $mostcommon, and your values rolled were ";
for ($i=0;$i<count($rolls);$i++) {
if ($i>=1) {
echo(", ");
}
echo($rolls[$i]);
}
echo " on this $numsides-sided dice."
?>
And here's the description:
$numrolls = 10;
$numsides = 6;
Here we are defining the variables the script will use. Erase these if you plan on passing them through a form.
for ($i=0;$i<$numrolls;$i++) {
$rolls[$i] = rand(1,$numsides);
}
Here we are using a for statement to roll each die for every positive whole number below $numrolls, and adding the values to a variable. $i is incremented every time to get the correct placement for the variable, and to keep count.
for ($i=0;$i<count($rolls);$i++) {
$rollcounts = array_count_values($rolls);
}
Count the variables and write an array with each number counted. This will be used to find the most common number.
$mostcommon = 0;
for ($i=0;$i<count($rollcounts);$i++) {
if ($rollcounts[$i] >= $mostcommon) {
$mostcommon = $i;
}
}
We are first defining $mostcommon as zero, in case it, for some odd reason, was not automatically zero. Then, we are using the for to find out how many parts are in the value count array ($rollcounts), then calculating the most common roll.
echo "The most common roll was $mostcommon, and your values rolled were ";
for ($i=0;$i<count($rolls);$i++) {
if ($i>=1) {
echo(", ");
}
echo($rolls[$i]);
}
echo " on this $numsides-sided dice."
Output the information that was requested. This is pretty self-explanitory. The for just loops through each roll to print it out.
Is this better?