473,898 Members | 4,144 Online

# Calculating the formula of a dice roll using rand?

29 New Member
Hi Guys!

So, I am using the modulus and a rand as part of a function I am writing. Now, I understand that:

Expand|Select|Wrap|Line Numbers
1. rand() % 6 + 1;
gives me a random number between one and six in this situation. However, I also know that

Expand|Select|Wrap|Line Numbers
1. rand();
gives me a random value from 0 - 32767 and
Expand|Select|Wrap|Line Numbers
1. srand;
changes the sequence

but I thought...

% = Whatever the remainder of a is after a / b.

... So, if you were to break
Expand|Select|Wrap|Line Numbers
1. rand() % 6 + 1;
up, what would it look like?

I need to make sense of this for my own good because I look at
Expand|Select|Wrap|Line Numbers
1. rand() % 6 + 1;
like this:

some random number / 6 = remainder left over from the random. Then add the one.

So, my question is two fold:

1 - how does
Expand|Select|Wrap|Line Numbers
1. rand()
get restricted to just 1 - 5 all of a sudden instead of the spectrum of numbers from 0 - 32767?

2 - Any of those numbers (1-5) divided by 6 gives you a fractional number, not a whole one and I thought modulus only works with whole numbers. What info am I missing here?

Feb 18 '13 #1
11 13806
Rabbit
12,516 Recognized Expert Moderator MVP
I'm not sure what you mean by "regular" arithmetic because modulus is a part of math.

There is no fraction return by a modulus. Yes, a number divided by another number can return a fraction. But that's not what a modulus returns. A modulus returns only the numerator of the fraction, and that is an integer.

7/6 = 1 and 1/6. The modulus returns 1.
8/6 = 1 and 2/6. The modulus returns 2.
15/6 = 2 and 3/6. The modulus returns 3.
And so on.
Feb 18 '13 #2
heiro1
29 New Member
Hi There,
Thanks for your reply! I am currently taking math95 along with CS161 so a modulus being a part of regular math is news to me. I never heard of it until mt CS161 class.

Anyway, although you pointed that out, I am still left with my original question. It sounds like you basically misunderstood it, that's not a hard thing to do considering I did a poor job trying to articulating my concern in the first place. I'll try to be more clear.

So, a % b = remainder of a. I already knew that from the start. What I was trying to ask was how do I make sense of
Expand|Select|Wrap|Line Numbers
1. rand() % 6 + 1;
in arithmetic terms because based on what I know about
Expand|Select|Wrap|Line Numbers
1. %
,
Expand|Select|Wrap|Line Numbers
1. rand() % 6 + 1;
makes no sense to me.

The reason why it doesn't make sense to me is because from what I read,
Expand|Select|Wrap|Line Numbers
1. rand() % 6 + 1;
is supposed to give you a random number between 1 and 6. However, I look at
Expand|Select|Wrap|Line Numbers
1. rand() % 6 + 1;
like this :

some random number / 6 = remainder left over from a. Then + one.

... So, how does
Expand|Select|Wrap|Line Numbers
1. rand()
get restricted to just 1 - 5 all of a sudden? instead of the spectrum of numbers from 0 - 32767?

... Also, any of those numbers divided by 6 gives you a fractional number (i.e. 1/6 = 0.16), not a whole number and I thought modulus only works with whole numbers.

I know how to use this function, I would just like to understand how these numbers are treated.
Feb 18 '13 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
This code:

Expand|Select|Wrap|Line Numbers
1. rand() % 6 + 1;
2.
Remember, the modulus operator provides the remainder when dividign by 6. Therefore, rand() % 6 returns a value between 0and 5. Your dice are numbered 1 to 6.
Feb 18 '13 #4
donbock
2,426 Recognized Expert Top Contributor
The modulus operation (%) has a higher precedence than addition, so the expression will do what you want. However, I personally prefer to use parentheses to clarify precedence for operators that I don't use very often. Thus, I personally would prefer
Expand|Select|Wrap|Line Numbers
1. (rand() % 6) + 1;
Note: I do this purely for my own benefit when reading the code. These parentheses do not alter the behavior of the compiler.

By the way, rand() returns a value between 0 and RAND_MAX. RAND_MAX is guaranteed to be greater than or equal to 32767. Let's assume RAND_MAX is 32767. Then the mod-6 remainder will be...
• 0 for 5462 values between 0 and RAND_MAX;
• 1 for 5462 values between 0 and RAND_MAX;
• 2 for 5461 values between 0 and RAND_MAX;
• 3 for 5461 values between 0 and RAND_MAX;
• 4 for 5461 values between 0 and RAND_MAX;
• 5 for 5461 values between 0 and RAND_MAX;
Your dice will be slightly weighted towards 1 and 2 (after adding one to the mod-6 result). The actual distribution will vary based on the actual value of RAND_MAX in your compiler.
Feb 18 '13 #5
heiro1
29 New Member
So, does rand() % 6 know to return a value of 1-5 in the first place? 1-5 is supposed to be the result (plus one of course). I mean, 6 / 32767 = 5461.16 +1 = 5462 true, but how does this function then, take the number 5462 and spit out 1-5 (plus 1)? That is the crux of my confusion.
Feb 18 '13 #6
heiro1
29 New Member
My book wasn't clear on whether or not rand was assigned to be between 1-6 or not. So, I assumed the function had to come to 1-5(+ 1) on its own.
Feb 18 '13 #7
weaknessforcats
9,208 Recognized Expert Moderator Expert
Just divide 5462 by 6. You get 5460 and a remainder of 2.

So 5462 % 6 is 2.

The only possible remainders are 0 through 5.

OK. If you add 1 to a value between 0 and 5 you get a value between 1 and 6. Right?

Your statement containing 6 / 32767 troubles me. The modulus operator does 32767/6 and not the other way around. Also,
5461.16 is confusing since there are no decimals in integer arithmetic.
Feb 18 '13 #8
donbock
2,426 Recognized Expert Top Contributor
The modulo operator is defined such that (A % B) evaluates to the remainder of A divided by B. For A and B both positive, the rules of mathematics tell us that the remainder has to be greater than or equal to 0 and less than the quotient (B). That is, the remainder will be between 0 and B-1.

If both arguments are positive, then the right-side argument (B=6) determines the range of the modulo result. the left-side argument (A) determines which value within that range you get.

I did not suggest that the modulo expression will "take the number 5462 and spit out 0-5". What I meant is that 5462 different output values from rand() [0,6,12,...,3276 0,32766], when modulo'ed by 6, will yield a result of 0; same for a result of 1. However, only 5461 different output values from rand(), when modulo'ed by 6, will yield a result of 2 [2,8,14,...,3275 6,32762]; same for a result of 3,4, or 5. So the odds of rolling a 1 (0+1) are 5462/32768 while the odds of rolling a 3 (2+1) are 5461/32768. So 1 is ever so slightly more likely than 3.
Feb 18 '13 #9
heiro1
29 New Member
Ok, I'm processing that..
Feb 18 '13 #10