dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: rand() problems, help!

  1. #1
    Join Date
    Feb 2011
    Posts
    16

    rand() problems, help!

    So ive been trying to create random numbers for a game I am making, here is my code:

    Code:
    #include <iostream>
    #include <ctime>
    
    using namespace std;
    
          int rnum;
          int rnum2;
    
    class Number{
          public:
        
          
          
          void numberr(){
               srand(time(0));
               rnum=rand()&#37;20;
               }
          void number2r(){
               rnum2=rand()%20;
               }
          void shownum(){
               cout<<rnum<<"\n";
               cout<<rnum2<<"\n";
               }
               
          };
          
          
    int main(){
        Number number;
        int i;
        number.numberr();
        number.number2r();
        number.shownum();
        number.numberr();
        number.number2r();
        number.shownum();
        
    
        system("PAUSE");
        return 0;
    }
    Whenever I call numberr() or number2r() more than once per second it generates the same number. How can I get it to generate a new number every time I call numberr() even if a second has not gone by?

    Thanks
    Zak
    Last edited by Marc G; February 15th, 2011 at 10:17 AM. Reason: Added code tags

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,559

    Re: rand() problems, help!

    Call srand once when your program starts. Don't call it every time you call rand.

    Please use code tags when you post code.

  3. #3
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,151

    Re: rand() problems, help!

    Also, it's not recommended to use system("pause").
    See Pause Before Exiting a Console Application
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  4. #4
    Join Date
    Feb 2011
    Posts
    16

    Re: rand() problems, help!

    Should I call srand at the beginning of int main() or atr the start of my class.
    PS how do you post your code to look like code?

    Zak

  5. #5
    Join Date
    Feb 2011
    Posts
    16

    Re: rand() problems, help!

    Oh ok so I put srand(time(0)) at the beginning of main() and it is working now. Thanks for the help.

    Zak

  6. #6
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,151

    Re: rand() problems, help!

    Yes correct, you put srand() at the beginning of your program, your main() function in your case.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,559

    Re: rand() problems, help!

    Quote Originally Posted by Zakwak View Post
    Should I call srand at the beginning of int main() or atr the start of my class.
    PS how do you post your code to look like code?

    Zak
    put code and /code between brackets []

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)