| | |
Why is it dividing when no division operator is being used?
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 9
Reputation:
Solved Threads: 1
I am trying to build a random number generator based on dice. Yes, I know there are ones out there but I am trying to do this whole programming thing for myself... I thought I had done it right but it keeps telling me "Warning: Division by zero" when I am not using any division, can anyone explain and give me some advice?
Forgive me, I am a bit new here so any other info needed I can give you
PHP Syntax (Toggle Plain Text)
<? srand(time()); $random = (rand()%$POST_number)+1*$POST_how+$POST_str+$POST_mod; print("You just rolled a D6 and your roll is: $random"); ?>
Forgive me, I am a bit new here so any other info needed I can give you
the modulus operator (%) takes the left side, divides it by the right side, and returns the remainder. So, 5%3 would result in two, since 5/3 yields a remainder 2.
If you are getting a division by zero error, that means the value on the right side of the equation is 0. Debug the value of your '$POST_number' variable, your error lies there.
Consider the following for random number generation emulating dice rolls:
the above creates a min and max and uses the built-in functionality of rand to return the roll.
the above does the same as the previous, just with less code. the range function creates an array of numbers starting with the first argument, ending with the last. The above example has created an array of twenty elements , values 1-20. The shuffle function takes that array and randomizes the order of its elements. The nice thing about shuffle, is that it seeds the random number generator automatically (srand function). By printing any element of the array after shuffling (I print the first one here), you get a random number within the range.
If you are getting a division by zero error, that means the value on the right side of the equation is 0. Debug the value of your '$POST_number' variable, your error lies there.
Consider the following for random number generation emulating dice rolls:
php Syntax (Toggle Plain Text)
srand(time()); $minimum = 1; $maximum = 20; $roll = rand($minimum , $maximum); print "You rolled: $roll";
the above creates a min and max and uses the built-in functionality of rand to return the roll.
php Syntax (Toggle Plain Text)
$range = range(1,20); shuffle($range); print "You rolled: ".$range[0];
the above does the same as the previous, just with less code. the range function creates an array of numbers starting with the first argument, ending with the last. The above example has created an array of twenty elements , values 1-20. The shuffle function takes that array and randomizes the order of its elements. The nice thing about shuffle, is that it seeds the random number generator automatically (srand function). By printing any element of the array after shuffling (I print the first one here), you get a random number within the range.
The End
![]() |
Similar Threads
- Using Division Operator (C++)
- C++ Division Remainder Help (C++)
Other Threads in the PHP Forum
- Previous Thread: Validating mail function
- Next Thread: Open links of the same domain to open in the same window
| Thread Tools | Search this Thread |
Tag cloud for PHP
# .htaccess 5.2.10 access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl database date directory display dissertation download dynamic echo email error file files folder form forms function functions google href htaccess html image images include insert integration ip java javascript joomla ldap legislation limit link login loop mail menu mlm mod_rewrite multiple mysql mysqlquery oop open parse paypal pdf persist php problem query radio random recursion regex remote script search server sessions sms soap sockets source space sql structure syntax system table tutorial update upload url validation validator variable video web xml youtube






