rand() problem

This is a discussion on rand() problem within the C++ Programming forums, part of the General Programming Boards category; OK, Im trying to make a game that rolls die... currently i have tried doing rand() by time... but this ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    76

    rand() problem

    OK, Im trying to make a game that rolls die... currently i have tried doing rand() by time... but this causes the same seconds to be used in srand() because of compile time, which in turn makes the same values for each die.
    How would I go about randomizing 2 integer variables in the same program instance?

    Code:
    #include <iostream>
    #include <time.h>
    
    using namespace std;
    
    void Menu();
    void GameLoop();
    int rollDie();
    int randomize();
    int firstRoll();
    void makePoint(int point);
    
    int die1;
    int die2;
    int diesum;
    
    bool done=false;
    
    int main()
    {
        Menu();
        cin.get();
        return 0;
    }
    
    void Menu()
    {
        char answer;
        cout<<"\n\t\tWelcome to Craps!\n";
        cout<<"\t\t  (P) to Play\n";
        cout<<"\t\t  (Q) to Quit\n";
        cout<<"\t\t   -->: ";
        cin>>answer;
        if(answer == 'P' || answer == 'p')
        {
                       GameLoop();
        }
        if(answer == 'q' || answer == 'Q')
        {
             cout<<"\t\tThank You for playing Craps!\n";
             cout<<"\t\t       Quitting.....";
             done=true;
        }
    }
    
    void GameLoop()
    {
        int result;
        while(!done)
        {
                    result=firstRoll();
                    makePoint(result);
        }
    }
    
    int randomize()
    {
        time_t seconds;
        srand((unsigned)time(&seconds));
        return (rand() % (6-3+1*3)+1);
    }
                
    int rollDie(int die1, int die2)
    {
        int diesum;            
        die1 = randomize();
        die2 = randomize();  
        diesum=die1+die2;
        cout<<"\t\tYou have rolled a "<<die1<<" and a "<<die2<<"\n";
        cout<<"\t\tWith a sum of >>>> "<<diesum<<" <<<<"<<"\n";
        return diesum;
    }
    
    int firstRoll()
    {
        int die1, die2;
        diesum = rollDie(die1,die2);
        
        if(diesum == 7 || diesum == 11)
        {
             cout<<"\t\t\tYou win!\n";
             cin.get();
             return 0;
        }
        else if(diesum == 2 || diesum == 3 || diesum == 12)
        {
             cout<<"\t\t\tYou lose!\n";
             cin.get();
             return -1;
        }
        else
        {
             cout<<"\t\t\tKeep rolling!\n";
             cin.get();
             return diesum;
        }
    }
    
    void makePoint(int point)
    {
       done=true;
       cout<<"\t\tMakePoint!";
    }
    Any suggestions on newbish techniques? or other ways of doing specific tasks?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Don't call srand more then one in your program. Use srand(time(NULL)).

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    One of the regular posters on here, Narue, has written a good article about random numbers - take a look here: http://www.eternallyconfuzzled.com/articles/rand.html

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Quote Originally Posted by Bench82
    One of the regular posters on here, Narue, has written a good article about random numbers - take a look here: http://www.eternallyconfuzzled.com/articles/rand.html
    Known here as Prelude.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    76
    but even if i take the extra srand() out... arent the same seeds being called whenever i call rand() twice, generating the same number?

    and that article seems a little much for what i am wanting to do...unless you pros tell me that thats how i should do it...

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Seeding a random number generator is only done once. That's what seed means. It gives it an initial value, and from that initial value it keeps generating random numbers. Every time you call rand() you will get another random number. The generator is able to come up with different numbers each time because it modifies its internal data each time you call rand() to set itself up for the next call to rand().

    Test it out. Call srand() once in a simple program, then run a loop that outputs the result of rand() over and over. Try passing the same seed to srand() every time you run your program instead of time(0). You will end up with the same sequence of numbers because setting the seed restarts the random number generation mechanism.

    And yes, the stuff in that article is probably not necessary for what you want to do. Simply using rand() % 6 + 1 should be fine.

  7. #7
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by gL_nEwB
    but even if i take the extra srand() out... arent the same seeds being called whenever i call rand() twice, generating the same number?

    and that article seems a little much for what i am wanting to do...unless you pros tell me that thats how i should do it...
    rand() alwais generates the same sequence of numbers. srand() just sets the starting point.
    Kurt

  8. #8
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Maybe running this would help to clarify:
    Code:
    #include<iostream>
    #include <ctime>
    
    int main()
    {
      for (int i=0;i<10;i++)
      {
      time_t start = time(0);
      srand(start);
      std::cout<<i<<": "<<rand()<<std::endl<<std::endl;
      while(start==time(0)); //wait until time changes
      }
      for (int i=0;i<10;i++)
      {
        time_t start = time(0);
        std::cout<<i<<": "<<rand()<<std::endl<<std::endl;
        while(start==time(0));
      }
    }
    On my system, the values in the first loop increase by three, while the values in the second loop are much more varied
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21