GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Feb-2007, 13:31
jj9187 jj9187 is offline
New Member
 
Join Date: Feb 2007
Posts: 1
jj9187 is on a distinguished road

Random Number Generator Help


I'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:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char *argv[])
{
  // Number of times to roll the set of dice
  int diceRolls;
  // The number the user wants statistics on
  int userNumSelection; 
  // Will be used to end the program if the user desires.
  char keepgoing; 
  
  do 
{
  //Prompt the user for how many times to roll the dice
  printf("How many times would you like to roll the set of dice?\n");
  
  //Input users answer into diceRolls
  scanf("%d", &diceRolls);
  
  //Prompt the user for which number they want to know information about
  printf("What number do you want the statistics for?\n");
  
  //Input users answer into userNumSelection
  scanf("%d", &userNumSelection);
  
  //Displays the statistics the user desires to know
  printf("The Number %d was rolled __ times.\n",userNumSelection);
  
  printf("Do you wish to continue? (y = yes, n = no).\n");
  scanf("%c", &keepgoing);
  
} while (keepgoing == 'y');

  //Pauses so that you can see the output before the system exits
  system("PAUSE");	
  
  // Signals the end of the program to the computer
  return 0;
}
  #2  
Old 12-Feb-2007, 16:17
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,496
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Random Number Generator Help


Quote:
Originally Posted by jj9187
II 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.

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

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:
#include <stdio.h>    /* for the usual I/O                   */
#include <time.h>     /* for time()                          */
#include <stdlib.h>   /* for rand() and srand() and RAND_MAX */
.
.
.
.
int main()
{
/* all of your variable declarations
 */
.
.
.
/* 
 *  Do the following one time in your program, some place
 *  before you call rand() for the first time
 */
    srand(time(0));
.
.
.



}
Regards,

Dave
  #3  
Old 12-Feb-2007, 23:25
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,377
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Random Number Generator Help


A 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 GIDBlogPh.D. progress by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 18:43.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.