Jump to content
  • Advertisement
Sign in to follow this  
one mind

How to work out a percentage?

This topic is 4292 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have written a dice roll progam that rolls a 6 sided dice 1000000 times and i need to work out the percentage each number comes out. For example, the numbers come out: 1 = 166430 0.0% 2 = 166639 0.0% 3 = 167123 0.0% 4 = 166621 0.0% 5 = 166416 0.0% 6 = 166771 0.0% So to work out the percentage 1 comes out, i divide 166430 by 1000000 to get 0.16643 which i could round to 16.64% for my program. However, my program outputs 0.0% Is this a computer arithmetic problem? Here is the code i use to do the calculation: double percent = 167036 / 1000000; but when i ouput percent it equalt 0.0 Any help would be greatly appreciated. Thanks :)

Share this post


Link to post
Share on other sites
Advertisement
Actual percent would be this:

unsigned int totalRolls = 1000000, rolled1 = 166430;

double percent1 = (double(rolled1) / totalRolls) * 100;

In your algorythm computation is made with integer numbers, because both operands of division are integer numbers. If at least on of them is of floating point number (float or double), then all computation is performed with floating point numbers. So, like in my excample - cast one of them to double and you'll get the desired result.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!