Thread: Generating Random Numbers

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    117

    Generating Random Numbers

    Hi, I'm trying to make a program to play the game Craps. The rules of the game are working fine, but whenever I run the program, it generates the same random numbers EVERY time.

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <time.h>
    
    using namespace std;
     
     int roll();
     
     int round = 1;
     int die1;
     int die2;
     int total;
     int point;
     int rollNumber;
     int numberWon = 0;
     int numberLost = 0;
     bool roundWin;
     bool roundOver;
     
     
     
     //BEGIN MAIN
     int main() {
         
         while(round <= 10) {
                     roundWin = false;
                     roundOver = false;
                     roll();
                     
                     if(total == 7 || total == 11) {
                              roundWin = true;
                              roundOver = true;
                              cout<<"WIN!"<<endl;
                     }
                     else if(total == 2 || total == 3) {
                              roundOver = true; 
                              cout<<"LOSE!"<<endl;    
                     }
                     else {
                              point = total;     
                     }
                     
                     if(roundOver != true) {
                              
                              do {
                                 roll();    
                              }  while (total != 7 && total != point);
                              
                              if(total == point) {
                  	  		  		   roundWin = true;
                                       roundOver = true;  \
                                       cout<<"WIN!"<<endl;       
                              }                         
                              
                              else if(total == 7) {
                                   roundOver = true;
                                   cout<<"LOSE!"<<endl;
                              }   
                     }
                     
                     if(roundWin == true)
                                 numberWon++;
                     else if(roundWin == false)
                                 numberLost++;
                     round++;
    
         }
         
         cout<<"OVERALL: "<<endl;
         cout<<numberWon << " wins and " << numberLost << " losses" <<endl;
         system("PAUSE");
         
         return 0;
         
         
    }    
    
     int roll() {
         time_t seconds;
         time(&seconds);
         srand((unsigned int) seconds);
         
         die1 = (rand() % 6) + 1;
         die2 = (rand() % 6) + 1;
         total = die1 + die2;
         rollNumber++;
         
         cout<<die1 << " " << die2<<endl;       
         
     }

    Some unused variables are up there, I know. I'm mainly just concerned with the roll() function because it keeps giving me the same numbers for all rolls. I tried doing the following instead --

    Code:
    srand(rand());
    This gave me 10 different rolls, but everytime I ran the program it still gave me the same results. How can I create new random numbers everytime I call the roll() function?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't seed rand with rand. You seed it with something like time. Otherwise, you're seeding it with the same value every time (rand, called without having been seeded, will produce the same first result every time).


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    Wouldn't seconds change each time roll() is called though?

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    For a little more detail on what quzah is talking about, see here and here.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    37
    if u wanna know more about random number generation read this:
    Mersenne Twister Random Number Generator

  6. #6
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    use srand() with the current time, or write a fancy random number generator class that self seeds with data from random.org (p.s. that's what I use)

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by SterlingM View Post
    Wouldn't seconds change each time roll() is called though?
    To answer your actual question: it might. But computers are fast these days, and a slow computer can run your function in milliseconds. So there's a good chance that the time is still the same every time you call rand.
    It's unwise to seed in the function anyway. Just seed in the main function...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with displaying random numbers
    By Bumps in forum C++ Programming
    Replies: 12
    Last Post: 10-03-2009, 12:29 PM
  2. Generating random numbers...
    By naspek in forum C Programming
    Replies: 7
    Last Post: 07-29-2009, 02:00 PM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. Random Number Generating
    By K.n.i.g.h.t in forum C Programming
    Replies: 9
    Last Post: 01-30-2005, 02:16 PM
  5. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM