Jump to content

php dice rolling


helen11

Recommended Posts

HI I have put together a php script that rolls a dice 26 times and assigns each result in turn along with its roll number. What I want to do now is print the total roll value and the average roll value but as i'm new to php I dont know how to do this so any help would be greatly appreciated. This is what I have so far

<head>
<title>Dice Roll</title></head>
<body>
<?php

$rolls = 26;

$sides = 6;

$results = array_fill(1, $sides, 0);

for($i=1; $i<=$rolls; $i+=1)
{

$roll = rand(1, $sides);

$results[$roll] += 1;
}

print '<table><tr><th>Roll Number</th><th>Roll Value</th></tr>';

for($i=1; $i<=$sides; $i+=1) 
{ 
    printf('<tr><td>%d</td><td>%d</td></tr>', $i, $results[$i]);
}
print '</table>';  
?>
</body>
</html>

Link to comment
Share on other sites

I'd do something like this

 

<?php

$rolls = 26;
$sides = 6;

$results = array();

for($i = 0; $i < $rolls; $i++) {
$results[] = rand(1,$sides);
}

echo "Each roll:<ul><li>";
echo implode("</li><li>", $results);
echo "</li></ul>";

$total = array_sum($results);

echo "Total: $total<br>";
echo "Average: ". $total / count($results);

?>

Link to comment
Share on other sites

That doesn't seem to work :( this is what I need to do:

 

Use two separate for loops to roll a dice 26 times and assign each result in turn along with its roll number to a two dimensional array in the first for loop then count the total value of dice rolls over 26 throws and print to an html table with a header row using a second program loop. The total value of the dice rolls plus the average value per throw should be printed out.

 

This is what I have:

 

<?php

$aRolls = array();
$rolls = 26;
$sides = 6;

for ($i = 0; $i < $rolls; ++$i)
{
    $aRolls[] = rand(1, $sides);
}

$aValueCount = array_count_values($aRolls);

print '<table><tr><th>Roll Number</th><th>Roll Value</th></tr>';
for($i=1; $i <= $sides; ++$i) 
{ 
    printf('<tr><td>%d</td><td>%d</td></tr>', $i, $aValueCount[$i]);
}
print '</table>';

?>

Link to comment
Share on other sites

Mine does most of what you want, why must you use a 2D array?

 

Instead of imploding, use this

 

echo "Each roll:<ul>";
foreach( $results as $roll_number => $roll ) {
echo "<li>#$roll_number: $roll</li>";
}
echo "</ul>";

 

You're going to have to change this to table notation, but the logic is there.

Link to comment
Share on other sites

I believe whoever set the problem thinks that Key + Value constitutes two dimensions

 

I'd agree. The other issue I see with this problem is that it instills the assumption that in these types of situations you need to store the results in an array in order to do these types of operations. In this case you could just as easily create the output for the result of each die within the loop and add to a total variable. Then it's a simple operation to generate the average at the end. Far too often I see people loop through database results and dump them into an array only to later loop through the array to actually use the results.

Link to comment
Share on other sites

On reflection, the only 2D solution I could come up with is something like this, storing the throw numbers for each value

 

<?php
$rolls = 26;
$results = array(1 => array(), array(), array(), array(), array(), array());
$total = 0;

for ($i=1;  $i <= $rolls; ++$i) {
    $val = rand(1,6);
    $results[$val][] = $i;
    $total += $val;
}

echo "<table border='1'>";
echo "<tr><th>Value</th><th>Throws</th><th>Count</th></tr>";

foreach ($results as $val => $throws) {
    echo "<tr><th>$val</th><td>" . join(', ', $throws) ."</td><td>" . count($throws) . "</td></tr>";
}

echo "</table><br />";
printf ('Total : %d, Average: %4.2f', $total, $total/$rolls);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.