zero-perfoliate
zero-perfoliate

Author Topic: Random Numbers, Arrays, Totals  (Read 323 times)

Offline biznacho

  • New PHP Members
  • Posts: 1
  • Karma: +0/-0
Random Numbers, Arrays, Totals
« on: August 30, 2009, 10:15:59 AM »
I need help with what seems to be a simple problem, but I've tried for a few hours to figure out the solution with no luck on my own. I can't seem to find anything through google that covers this specific issue, or I've not refined my searches enough to find anything. The situation is that I want to generate a series of random numbers, then total the numbers generated. I can easily get my numbers, but where I think I am missing the point is that I think I need to insert the numbers into an array, then do an array_sum. I'm either getting the result "Array" though, or "Undefined offset."

Here is the code I'm using (getting the numDice and typeDice from an html form):

The number of dice to roll:
$numDice = $_POST['numDice'];
echo "<p>The number of dice rolled: " . $numDice. "</p>";

The type of dice to roll:
$typeDice = $_POST['typeDice'];
   echo "<p>The type of dice rolled: " . $typeDice . " sided.</p>";

$rollNum = '';
for ($rollNum=1; $rollNum <= $numDice; $rollNum++)
{
   // create variable to display random rolls
   $random = mt_rand(1, $typeDice);
         
   // display dice rolls
   echo "Roll " . $rollNum . ": " . $random . "
";

So far this works to display the basic dice rolls, but I would like to total the rolls at the end. I'm eventually going to use a db and insert the values, and I know how to sum the values in MySQL, but I'm trying to do it with basic PHP scripting in this instance without relying on a db.

Can someone please point me in the right direction to do what I need to do?

Thanks in advance.

Brian