helen11 Posted June 27, 2012 Share Posted June 27, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/264881-php-dice-rolling/ Share on other sites More sharing options...
xyph Posted June 27, 2012 Share Posted June 27, 2012 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/264881-php-dice-rolling/#findComment-1357439 Share on other sites More sharing options...
helen11 Posted June 27, 2012 Author Share Posted June 27, 2012 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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/264881-php-dice-rolling/#findComment-1357444 Share on other sites More sharing options...
Pikachu2000 Posted June 27, 2012 Share Posted June 27, 2012 Oh, so this is homework. Got it. Quote Link to comment https://forums.phpfreaks.com/topic/264881-php-dice-rolling/#findComment-1357445 Share on other sites More sharing options...
xyph Posted June 27, 2012 Share Posted June 27, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/264881-php-dice-rolling/#findComment-1357448 Share on other sites More sharing options...
helen11 Posted June 27, 2012 Author Share Posted June 27, 2012 Because i've been told to use a 2d array. It still won't work i'm ready to pull my hair out. Quote Link to comment https://forums.phpfreaks.com/topic/264881-php-dice-rolling/#findComment-1357449 Share on other sites More sharing options...
Barand Posted June 27, 2012 Share Posted June 27, 2012 I believe whoever set the problem thinks that Key + Value constitutes two dimensions Quote Link to comment https://forums.phpfreaks.com/topic/264881-php-dice-rolling/#findComment-1357459 Share on other sites More sharing options...
Psycho Posted June 27, 2012 Share Posted June 27, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/264881-php-dice-rolling/#findComment-1357467 Share on other sites More sharing options...
Barand Posted June 27, 2012 Share Posted June 27, 2012 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/264881-php-dice-rolling/#findComment-1357472 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.