473,898 Members | 4,144 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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?

As you can see I am confused about this. Help is always appreciated :)
Feb 18 '13 #1
11 13806
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
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
9,208 Recognized Expert Moderator Expert
This code:

Expand|Select|Wrap|Line Numbers
  1. 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.
Feb 18 '13 #4
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
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
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
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
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
29 New Member
Ok, I'm processing that..
Feb 18 '13 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

by: lawentzel | last post by:
I am new to PHP and am an avid RPG gamer. I am looking to use PhpMyChat to be able to game with friends all over the US. With that having been said, PhpMyChat seems to offer a lot of what I am looking for. It allows you to send a message to individual players (chatters). It allows for actions using "/me". The only thing it really needs is the capability to do a dice roll. Can someone point me to a site which might have some examples...
by: Tom McCallum | last post by:
Can any tell me why rand() is such a bad pseudo-random number generator. In all the articles I have read they say that you can predict the outcome of rand() but when I used its output with NIST's random number testsuite it passed all of the tests thrown at it for randomness. Can anyone suggest a test I could use that it would fail? A pointer to a suitable article would be appreciated. Thanks for your help in advance, Tom
by: Chris Gordon-Smith | last post by:
I have a simulation program that calls the rand() function at various points while it executes. For the user interface that displays statistics etc. while the program runs, I use the Lazarus GUI library, which is based on GTK. Depending on which libraries I link in, I can have either a GTK1 or GTK2 user interface. I'm finding that the simulation behaves differently depending on whether I link with GTK1 or GTK2.
by: Richard Gomez | last post by:
I would like to have a persons age in my database using their date of birth. Can anybody give me some sugestios how i can do this
by: keith katthy | last post by:
#include <iostream> #include <conio.h> cpp. How to get the random number in matrix like , the number inside the , and get the value? #include <cstdlib> #include <math.h> #include <stdio.h>
by: clairelee0322 | last post by:
Hello guys again! I am a C++ beginner and I am working on another project.. It's called Dice Lab with Loops.. unforunately I don't even know how to start the program.... Don't blame me for not pay attention in the class.. Seriously, my teacher just ask us to do the sample programs in the textbook and prepare for the finals... Everytime I ask him question... he has to open the textbook to find the answer.. I've been searching for dice...
by: clairelee0322 | last post by:
I am a c++ beginner and i am working on a dice loop lab. This program should roll two dice (each dice is from 1 to 6) and then total them together. The user should be able to input how many times they would like to have the dice rolled. Your program should output the rolls and once the rolls are done, it should output how many of each number had been rolled. //this program will run forever and never stops expect i input the number than is...
by: TamaThps | last post by:
I have to write a program that lets the user play the game of Craps. As of right now it is incomplete and I have run into some errors. It's my first time using functions instead of the program all being in the int main(). I am using visual studio on windows XP. In my code I've defined a local variable in a function but when I run the program and it gets to that function it says I have an undeclared variable. The code for the program...
by: matt murray | last post by:
I'm writing a text based adventure game and was wondering if there was a way to "Roll Dice". An example of what i am trying to use this for would be attacking or defending in the game. If they get above or equal to a certain number It would be considered a hit. I would also need this same automated dice rolling for the enemies in the game. Thanks Matt.
by: Dave067 | last post by:
Hi Folks! I was trying to write some Python script to calculate resistor values for electronics projects, and wanted to generate lists of the preferred resistor values in each standard commercially available ranges of values (E6, E12, E24 etc.) by formula rather than using large unwieldy lists or directories.. From what I've been able to find by reading & Googling, the "E series" of preferred values are based on a logarithmic scale /...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.