RSS Forums RSS
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 355 | Replies: 7 | Solved
Reply
Join Date: Feb 2008
Posts: 8
Reputation: Orbit2007 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Orbit2007 Orbit2007 is offline Offline
Newbie Poster

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

  #1  
Jul 9th, 2008
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?

<?	
	
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2008
Location: Sweet India
Posts: 977
Reputation: Shanti Chepuru is on a distinguished road 
Rep Power: 2
Solved Threads: 90
Shanti Chepuru's Avatar
Shanti Chepuru Shanti Chepuru is offline Offline
Posting Shark

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

  #2  
Jul 9th, 2008
I think you % symbol is giving you that error..
Reply With Quote  
Join Date: Jun 2008
Location: Delhi
Posts: 119
Reputation: vicky_rawat is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 15
vicky_rawat's Avatar
vicky_rawat vicky_rawat is offline Offline
Junior Poster

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

  #3  
Jul 9th, 2008
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.
Vivek Rawat
Keep solving complexities.
Reply With Quote  
Join Date: Feb 2008
Posts: 8
Reputation: Orbit2007 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Orbit2007 Orbit2007 is offline Offline
Newbie Poster

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

  #4  
Jul 9th, 2008
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
Reply With Quote  
Join Date: Feb 2008
Posts: 8
Reputation: Orbit2007 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Orbit2007 Orbit2007 is offline Offline
Newbie Poster

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

  #5  
Jul 10th, 2008
Anybody else have any thoughts? I took the percent sign out and can only get 0 for an answer
Reply With Quote  
Join Date: Nov 2007
Location: Perth, Australia
Posts: 119
Reputation: Auzzie is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 16
Auzzie Auzzie is offline Offline
Junior Poster

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

  #6  
Jul 12th, 2008
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
Reply With Quote  
Join Date: Nov 2007
Location: Las Vegas, Nevada
Posts: 83
Reputation: johnsquibb is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 14
johnsquibb's Avatar
johnsquibb johnsquibb is offline Offline
Junior Poster in Training

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

  #7  
Jul 12th, 2008
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:

  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.

  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.
The End
Reply With Quote  
Join Date: Feb 2008
Posts: 8
Reputation: Orbit2007 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Orbit2007 Orbit2007 is offline Offline
Newbie Poster

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

  #8  
Jul 12th, 2008
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 1:53 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC