Calculating the formula of a dice roll using rand?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • heiro1
    New Member
    • Jan 2013
    • 29

    Calculating the formula of a dice roll using rand?

    Hi Guys!

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

    Code:
    rand() % 6 + 1;
    gives me a random number between one and six in this situation. However, I also know that

    Code:
    rand();
    gives me a random value from 0 - 32767 and
    Code:
    srand;
    changes the sequence

    but I thought...

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

    ... So, if you were to break
    Code:
    rand() % 6 + 1;
    up, what would it look like?

    I need to make sense of this for my own good because I look at
    Code:
    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
    Code:
    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?

    As you can see I am confused about this. Help is always appreciated :)
    Last edited by heiro1; Feb 18 '13, 05:40 AM. Reason: I needed to make the question more clear
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    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.

    Comment

    • heiro1
      New Member
      • Jan 2013
      • 29

      #3
      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
      Code:
      rand() % 6 + 1;
      in arithmetic terms because based on what I know about
      Code:
      %
      ,
      Code:
      rand() % 6 + 1;
      makes no sense to me.

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

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

      ... So, how does
      Code:
      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.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        This code:

        Code:
        rand() % 6 + 1;
        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.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          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
          Code:
          (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.

          Comment

          • heiro1
            New Member
            • Jan 2013
            • 29

            #6
            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.

            Comment

            • heiro1
              New Member
              • Jan 2013
              • 29

              #7
              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.

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                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.

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  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.

                  Comment

                  • heiro1
                    New Member
                    • Jan 2013
                    • 29

                    #10
                    Ok, I'm processing that..

                    Comment

                    • heiro1
                      New Member
                      • Jan 2013
                      • 29

                      #11
                      Ok, wow, thank you so much for that! You just put it into perfect perspective for me!! I appreciate it!

                      Comment

                      • ChristianHansen
                        New Member
                        • Aug 2023
                        • 1

                        #12
                        When you're doing rand() % 6 + 1, it's like you're saying "Give me a random number, divide it by 6 and keep the remainder, then add 1 to it." This neat little trick is how you get a range of 1 to 6 instead of the full spectrum.
                        And you're right, % usually works with whole numbers, but the remainder can be a fraction if the divisor is greater than the dividend. This is where the magic of truncating comes in, giving you those whole numbers in your desired range.
                        By the way, if you're curious about other random number adventures, you might want to check out the virtual d20 roll. It's like a playground for exploring randomness.

                        Comment

                        Working...