#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    C++ program random number generator problem


    Hello, I'm new to C++ and programming in general. I have been messing around with the basics to try and grasp them but sometimes I have to delve deeper to find something of interest.

    So the problem I have is that the current code I am using generates a "random" number. The number seems random but it comes back with the same list with each run of the program.

    Code:

    #include <iostream>
    #include <time.h>
    #include <unistd.h>

    using namespace std;


    int main()
    {
    int y = 0; //number of tries
    int input; //number the generator is trying to get that user specifies
    int ran; //number that generator makes
    cout << "Enter a number between 1 and 20\n";
    cin >> input;

    for(int ran = 0; ran != input; y++)
    {
    const int LOW = 1;
    const int HIGH = 20;
    time_t seconds;
    time(&seconds);
    ran = rand() % (HIGH - LOW + 1) + LOW;

    cout << ran << endl;
    //cout<<x<< endl;
    sleep(1);
    }

    cout << "The program tried " << y << " times to get the value " << input << endl;

    return 0;
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    33
    rand() was designed to generate repeatable sequences of "random" numbers.
    The sequences are repeatable because they are identified with a sequence number. If you don't choose a specific sequence number for your numbers, the compiler will use sequence number 1.

    To choose a specific sequence number (let's say sequence #1234567) use srand()

    Code:
    srand(1234567); /* use sequence #1234567 */
    To choose a different sequence every time you run your program, choose a different sequence number everytime :)

    A simple way to do that is to use the number of seconds since the Epoch as the sequence number.
    Code:
    srand(time(0)); /* use a different sequence number (almost) every time */
    Remember the prototype for srand() (and rand() is in <stdlib.h>) and the prototype for time() is in <time.h>.

    Note: changing the sequence number mid-way through your program effectively reduces the randomness of the generated random numbers, so you want to call srand() only once each time the program is executed.
    Last edited by bdb; October 24th, 2012 at 04:03 PM.
  4. #3
  5. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,548
    Rep Power
    2337
    You must seed the random number generator at the start of the program with srand().

    And when you post code, please use code tags.
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    62
    Rep Power
    3
    Originally Posted by Aaranos
    Hello, I'm new to C++ and programming in general. I have been messing around with the basics to try and grasp them but sometimes I have to delve deeper to find something of interest.

    So the problem I have is that the current code I am using generates a "random" number. The number seems random but it comes back with the same list with each run of the program.

    Code:
    #include <iostream>
    #include <time.h>
    #include <unistd.h>
    
    using namespace std;
    
    
    int main()
    {
        int y = 0; //number of tries
        int input; //number the generator is trying to get that user specifies
        int ran;   //number that generator makes
        cout << "Enter a number between 1 and 20\n";
        cin >> input;
        
        for(int ran = 0; ran != input; y++)
            {
                const int LOW = 1;
                const int HIGH = 20;
                time_t seconds;
                time(&seconds);
                ran = rand() % (HIGH - LOW + 1) + LOW;
                
                cout << ran << endl;
                //cout<<x<< endl;
                sleep(1);
            }
        
        cout << "The program tried " << y << " times to get the value " << input << endl;
        
        return 0;
    }
    Thinking about it, try srand() - see if that makes a difference.

    Regards,

    Shaun.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    62
    Rep Power
    3
    Surely there is a better way than doing this:
    Code:
    for(int ran = 0; ran != input; y++)
    Also, ran is already initialised to int before-hand in the code.

    Regards,

    Shaun.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0
    Originally Posted by bdb
    rand() was designed to generate repeatable sequences of "random" numbers.
    The sequences are repeatable because they are identified with a sequence number. If you don't choose a specific sequence number for your numbers, the compiler will use sequence number 1.

    To choose a specific sequence number (let's say sequence #1234567) use srand()

    Code:
    srand(1234567); /* use sequence #1234567 */
    To choose a different sequence every time you run your program, choose a different sequence number everytime :)

    A simple way to do that is to use the number of seconds since the Epoch as the sequence number.
    Code:
    srand(time(0)); /* use a different sequence number (almost) every time */
    Remember the prototype for srand() (and rand() is in <stdlib.h>) and the prototype for time() is in <time.h>.

    Note: changing the sequence number mid-way through your program effectively reduces the randomness of the generated random numbers, so you want to call srand() only once each time the program is executed.
    Thank you. Using that fixed it and after a few trials it appears to be completely random now. I'll have to do some more studying on some of these terms so I better understand how it works.

IMN logo majestic logo threadwatch logo seochat tools logo