Public Group

Need help understanding this logic....

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

Recommended Posts

Ok so I'm reading the book, Beginning C++ Through Game Programming, and i'm having trouble understanding a paragraph. I know what the code does what I cant seem to understand is how the author comes to the conclusion here is the paragraph"

"after generating a random number, randomNumber now holds a value between 0 and 32,747. But I need a number between 1 and 6, so next I use the modulus operator to produce a number in that range

int die = (randonNumber % 6) + 1; // I know how this works.

any positive number divided by 6 will give a remainder between 0 and 5"

That last line is where i get confused... for instance lets say the value in randomNumber is 22,465 that divided by 6 is going to be between 0 and 5. my result is remainder one so that works but what happens if the value of randomNumber is 32,747 the remainder would be 8. My math could be off. So my question is, can the value never be the max? mostly every other number would produce the right remainder but not the max number. I think I understand but I just need some more info to help explain this. another thing im not quite clear on is 6 goes into 10 once with the remainder 4 so how does the value 5 come into play? if anyone can clear this up for me I'd be very grateful...

Share on other sites
The remainder of 32747 divided by 6 is 5, not 8, so the remainder is within the range 0 to 5.

Share on other sites

The remainder of 32747 divided by 6 is 5, not 8, so the remainder is within the range 0 to 5.

Thanks for posting but you didnt really answer my question...

my caculator is saying the answer is
5457.833

even when i did it by hand that my result is
5457.822
wouldn't 8 be the remainder? because I'm assuming 6 goes into 32747, 5457 times but when i multiply 6 * 5457 i get 32742 why? I'm not bad at math usually im good but none of this is making sense right now.. it could be im tired and frusterated but I still want an explaniation..

never mind i've been looking at the answer the whole time.. sorry........ Edited by Shikamaru

Share on other sites
6 fits 5457.833 times in 32747. The number of whole sixes that fit into 32747 is 5457. 6 times 5457 is 32742, therefore the remainder is 5(32747 - 32742).

Share on other sites
Yes yes, i got that... thanks i was over complicating things for some reason thanks for the explaniation...

Share on other sites
It's the fraction that was throwing you. 5 / 6 = 0.83333

Share on other sites
Taking the modulus isn't simply just dividing by a number and see the result. Well kind of it is, but you have to remove the integer part and then multiply the decimal part with the same number again. Let us use your numbers as an exercise.

Number to operate on with modulus 6:

32747 / 6 = 5457.833333... (unlimited expansion of '3')

Remove the integer part and then multiply with 6.

0.833333... * 6 = 4.99998

Which you can see is very close to 5 but it is not exact.

All rational numbers can be written on the form m/n. (m divided by n.) The number 5457.833333... is rational because it can be written in a predictable way. You know there will be all number '3' for all eternity after the last digit.

Let us convert this number to a fraction:

x = .833333333333333… (This is the number we want on the form m/n.)
10x = 8.33333333… (Multiply by a number 10^n so that n makes the expansion of decimals overlap. n=1 suffices here since all the numbers are equal.)
10x - x = 9x = 8.333… - 0.8333… = 7.5 (If this is hard to see, just know that 0.333... minus 0.333... = 0, see below)

It is easier to see laid out like this:
[font=courier new,courier,monospace]. 8.333333333... (Had to add a dot in front to make this line align properly)[/font]
[font=courier new,courier,monospace]- 0.833333333...[/font]
[font=courier new,courier,monospace]= 7.500000000... [/font]

Then we have:

9x = 7.5
90x = 75
x= 75/90 = 5/6

Our fraction is 5/6 which makes 0.83333...

Then we can multiply our fraction by 6 to get the modulus:

5/6 * 6 = 5

Which indeed is 5. Exactly.

Final note: taking the modulus of a number can never result in either the same number as the modulus or a higher number. The result is always lower so having 8 as a result when the modulus is 6 is not possible. (It would mean you could still divide the number further, which is not a remainder at all.) Edited by aregee

Share on other sites
Very decent explaniation.. Thanks.. felt like I was back in school... I honestly would have never expect an answer like that lol.

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5
A4L
11

• 12
• 16
• 26
• 10
• 44
• Forum Statistics

• Total Topics
633768
• Total Posts
3013741
×