![]() |
|
#1
|
|||
|
|||
Random Number Generator HelpI'm attempting to write a program that asks the user how many times they would like to roll two dice.
Then it asks the user what number they would like to know information about. If they choose 5, it runs a random number generator the selected number of times (diceRolls), and then tells you how many times the number 5 was rolled. Frankly I don't understand random number generators and can't find a link to a "basic" explaination of them and how to limit them between 1 and 6. This is my first post here, so hopefully I've posted it in a manner that allows everyone to easily help me. Heres the code I've done so far. I always get the user prompts setup and stored first (since I know how to do that), and then work on the missing pieces I don't know. I've included the C code that I have so far, but it doesn't really have anything to do with the random number generator since I don't have an idea where to start. I've just posted it so that you can see the context better. CPP / C++ / C Code:
|
|||
|
#2
|
|||
|
|||
Re: Random Number Generator HelpQuote:
The standard C library function rand() generates an integer from zero up to and including RAND_MAX, where RAND_MAX is defined in <stdlib.h>. (It may be different for different compilers.) The idea is that you call rand() repeatedly to get numbers that are approximately uniformly distributed on the interval [0, RAND_MAX]. If you want to obtain an integer from rand() for a particular range, you can use the modulus operator '%'. If x and y are positive ints, then the value of the expression x % y is an integer that is greater than or equal to zero and less than y (that is, less than or equal to y-1.) Therefore, if you have z = rand() % 6;, the resulting value of z is greater than or equal to zero and less than or equal to 5. To get a number that is greater than or equal to 1 and less than or equal to 6, you can use CPP / C++ / C Code:
There are a couple of things that you should know about rand(): 1. It isn't really a "random" number generator, since the numbers are created by some kind of mathematical function. Each compiler and library distribution has its own version of rand(). So results with different compilers may be slightly different. Start with rand() supplied with your compiler. If it doesn't meet your requirements, then you can find examples of code for other "random" number generators on the web that may be more robust, statistically speaking. 2. If you just start calling rand() at the beginning of a program you will get the same sequence of ints from it every time you run the program. You can make it create a different sequence every time by "seeding" it with a number that is different every time. The function to "seed" it is called srand(), and here is a way to use current time (in seconds) to start it off: CPP / C++ / C Code:
Dave |
|
#3
|
||||
|
||||
Re: Random Number Generator HelpA couple minor additions:
Call srand() only once, at the beginning of the program. And mixing %d and %c using scanf() will cause problems. Read this series for more info. And while we're at it, check this out about system("pause"); __________________
The 3 Laws of the Procrastination Society: 1) Never do today that which can be put off until tomorrow 2) Tomorrow never comes |
Recent GIDBlog
Ph.D. progress by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Dice rolling program (random number generator) | crystalattice | Python Forum | 1 | 17-Apr-2006 17:34 |
| Converting a number amount to text | Godzilla | C++ Forum | 5 | 31-Mar-2006 11:38 |
| random number generation issues | Jonnyz007 | C++ Forum | 8 | 27-Oct-2005 19:13 |
| Need Help with my Cards Program (C++) | krisopotamus | C++ Forum | 2 | 06-Oct-2005 16:48 |
| Anyone can write a program code for this??? | chriskan76 | C Programming Language | 1 | 19-Oct-2004 20:25 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The