943,812 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 999
  • PHP RSS
Jul 9th, 2008
0

Why is it dividing when no division operator is being used?

Expand Post »
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?

PHP Syntax (Toggle Plain Text)
  1. <?
  2.  
  3. srand(time());
  4. $random = (rand()%$POST_number)+1*$POST_how+$POST_str+$POST_mod;
  5. print("You just rolled a D6 and your roll is: $random");
  6. ?>


Forgive me, I am a bit new here so any other info needed I can give you
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Orbit2007 is offline Offline
9 posts
since Feb 2008
Jul 9th, 2008
0

Re: Why is it dividing when no division operator is being used?

I think you % symbol is giving you that error..
Reputation Points: 137
Solved Threads: 162
Posting Virtuoso
Shanti C is offline Offline
1,641 posts
since Jul 2008
Jul 9th, 2008
0

Re: Why is it dividing when no division operator is being used?

Hi,

Can you please let us know, what is the value in $POST_number.

as % sign is for getting reminder.
So if $POST_number will have value 0, then it will give divide by 0 error.
Reputation Points: 28
Solved Threads: 19
Junior Poster
vicky_rawat is offline Offline
137 posts
since Jun 2008
Jul 9th, 2008
0

Re: Why is it dividing when no division operator is being used?

The current value is seven...it's just a number I chose. In the end it will be a user created number ranging from 1 to 24
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Orbit2007 is offline Offline
9 posts
since Feb 2008
Jul 10th, 2008
0

Re: Why is it dividing when no division operator is being used?

Anybody else have any thoughts? I took the percent sign out and can only get 0 for an answer
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Orbit2007 is offline Offline
9 posts
since Feb 2008
Jul 12th, 2008
0

Re: Why is it dividing when no division operator is being used?

Try splitting it down into sections see what happens there, and also try replacing rand() with say 5 and see if you still get the error
Reputation Points: 16
Solved Threads: 16
Junior Poster
Auzzie is offline Offline
121 posts
since Nov 2007
Jul 12th, 2008
0

Re: Why is it dividing when no division operator is being used?

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:

php Syntax (Toggle Plain Text)
  1. srand(time());
  2.  
  3. $minimum = 1;
  4. $maximum = 20;
  5.  
  6. $roll = rand($minimum , $maximum);
  7.  
  8. 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)
  1. $range = range(1,20);
  2. shuffle($range);
  3. 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.
Reputation Points: 14
Solved Threads: 14
Junior Poster in Training
johnsquibb is offline Offline
84 posts
since Nov 2007
Jul 12th, 2008
0

Re: Why is it dividing when no division operator is being used?

Thanks! I was able to get it to work! I toyed around with it the last few days and got it to somewhat work. My next problem I am going to post shortly.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Orbit2007 is offline Offline
9 posts
since Feb 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Validating mail function
Next Thread in PHP Forum Timeline: Open links of the same domain to open in the same window





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC