Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Dice rolling...
Index -> Programming, PHP -> PHP Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Squirter




PostPosted: Mon Jan 26, 2004 6:39 pm   Post subject: Dice rolling...

I'm trying to generate a number for a 20-sided die being rolled a certain amount of times.

code:

 <?
$dice = rand(1,20);
$rolls = $HTTP_GET_VARS['rolls'];

$outcome = $dice * $rolls;

echo "You want to roll a <b>20</b>-sided die <b> $rolls </b> times. <br>";
echo "The outcome is $outcome";

?>


Problem is, I want it to make a random number for each roll. Right now, it only gets one number, and multiplies it by the number of rolls I requested.

e.g Set to roll 3 times. First it rolls a 4. But then it multiplies itself by 3. 4 * 3 = 12.

I want it to make a different number each time, so:

Set to roll 3 times. 5 + 17 +12 = 34.

Anyone know the problem?
(BTW, I'm pretty new to PHP, so if you put in a new command, could you explain it? ^^Wink
Sponsor
Sponsor
Sponsor
sponsor
octopi




PostPosted: Mon Jan 26, 2004 8:27 pm   Post subject: (No subject)

I think you want to roll the dice, the number of times specifiyed by 'rolls'

consider the following:

code:
<?php
 $rolls = $HTTP_GET_VARS['rolls'];
 for ($x=1;$x <= $rolls; $x+=1) {
   $dice = rand(1,20);
   print "Roll $x: $dice<br>\n";
 }
?>


the 'for' command loops the number threw the stuff in the {}'s the number of times specified by the '$x=1;$x <= $rolls; $x+=1', this says....the first time we enter the for loop, set x=1, then the next parts the condition, it keeps looping until this is false, and the next parts the counter, this increments x by 1, each time, until it meets the condition.
Squirter




PostPosted: Tue Jan 27, 2004 10:05 pm   Post subject: (No subject)

Er, I didn't really understand that...
Could you give me another example?

BTW, what is <=?
PaddyLong




PostPosted: Wed Jan 28, 2004 3:17 pm   Post subject: (No subject)

<= is just less than or equal to... same as in any boolean comparison expression in any language
wtd




PostPosted: Sat Feb 14, 2004 12:13 am   Post subject: (No subject)

Just for the sake of havng an OO solution. Smile

code:
<?
   // Not tested

   class Die {
      var $sides;

      function Die($sides = 6) {
         $this->sides = $sides;
      }

      function roll() {
         return rand(1, $sides);
      }
   }

   $rolls = $HTTP_GET_VARS['rolls'];
   $die = new Die(20);
   for ($i = 1; $i <= $rolls; $i++) {
      echo('You rolled ' . $die->roll() . '<br/>');
   }
?>
Display posts from previous:   
   Index -> Programming, PHP -> PHP Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: