C++, rand()

Discussion in 'C++' started by Shayaan_Mustafa, Dec 25, 2010.

  1. Shayaan_Mustafa

    Shayaan_Mustafa New Member

    Joined:
    Dec 25, 2010
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Still a student.
    Location:
    Karachi
    My dear all,
    Hope you fine.

    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<stdlib.h>
    main()
    {
    cout<<"\n"<<endl;
    for(int i=0; i<8; i++)
    cout<<rand() << endl;
    cout<<"RAND_MAX = "<<RAND_MAX<< endl;
    getch();
    return 0;
    }
    
    Output:
    346
    130
    10982
    1090
    11656
    7117
    17595
    6415
    RAND_MAX = 32767



    Here is my code and its output. I am trying to generate random number as you see by for loop i<8, so my output contains 8 random numbers.
    Whenever I run program it generates the same output list as I posted above, every time I run.
    Now my question is why rand() generates the same output list at every run? Why don't it change the list of 8 random numbers? And in last line what is RAND_MAX = 32767?
    Can you explain for me please. Thanks.
     
    Last edited by a moderator: Dec 26, 2010
  2. jimblumberg

    jimblumberg New Member

    Joined:
    May 30, 2010
    Messages:
    120
    Likes Received:
    29
    Trophy Points:
    0
    To get rand to return different numbers you must first seed the random number generator by calling srand(). See this link: srand().

    That is the maximum value that your version of rand() can return. See this link: RAND_MAX

    Jim
     
  3. Shayaan_Mustafa

    Shayaan_Mustafa New Member

    Joined:
    Dec 25, 2010
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Still a student.
    Location:
    Karachi
    You said,"You must first sees". Please tell me what do you mean by this statement? Means what is seed? & what is srand() function?
    And also if RAND_MAX is return maximum value that my version supports then on other operating system this returned value must be different. Am I right?
     
  4. ThorAsgard

    ThorAsgard New Member

    Joined:
    Feb 16, 2010
    Messages:
    9
    Likes Received:
    3
    Trophy Points:
    0
    Location:
    Somerset,UK
    Code:
    //rand example
    #include <iostream>
    #include <stdlib.h>
    #include <time.h>
     
    using namespace std;
     
    int main()
    {
        //rand returns a number in the range 0 to RAND_MAX
        //RAND_MAX is a constant defined in <cstdlib.h>
        //its default value may very between implementations
        //but it is guaranteed to be a least 32767
     
        //srand is used to populate the random numbers returned by rand
     
        //initialise random seed
        srand(time(NULL));
     
        for(int nLoop = 0; nLoop < 8; nLoop++)
        cout << rand() << endl;
     
        return 0;
    }
    
    Hope the above code helps with your understanding of random number generating
    and the value of RAND_MAX
     
  5. Shayaan_Mustafa

    Shayaan_Mustafa New Member

    Joined:
    Dec 25, 2010
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Still a student.
    Location:
    Karachi
    Yes, you information helped me, but please tell me what is seed? what do you mean by seed? I don't understand by seed.
     
  6. ThorAsgard

    ThorAsgard New Member

    Joined:
    Feb 16, 2010
    Messages:
    9
    Likes Received:
    3
    Trophy Points:
    0
    Location:
    Somerset,UK
    Let me see if i can explain it more clearly
    rand returns a sequence of apparently non-related numbers each time it is called.
    The algorithm uses a seed to generate the series of numbers, which should be initialized to some distinctive value using srand.
    In non tech speek srand gives rand it's starting point for random number generation
    Hope this explains it more for you
     
    Shayaan_Mustafa likes this.
  7. jimblumberg

    jimblumberg New Member

    Joined:
    May 30, 2010
    Messages:
    120
    Likes Received:
    29
    Trophy Points:
    0
    Please read the following link. It should explain srand().

    srand

    Yes it can be different. On my system it returns 2147483647.


    Jim
     
    Last edited: Dec 27, 2010
    Shayaan_Mustafa likes this.
  8. Shayaan_Mustafa

    Shayaan_Mustafa New Member

    Joined:
    Dec 25, 2010
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Still a student.
    Location:
    Karachi
    Ok thanks I got your point.
    Now what is this that when I enter seed value as 1 then I got the same answer as the output given by my program that uses rand() function.
    ut as I enter different seed value I get different output.
    Why at 1 I am getting same answer as given by rand() function & the output is different at any other seed value? Why it is so?
     
  9. Shayaan_Mustafa

    Shayaan_Mustafa New Member

    Joined:
    Dec 25, 2010
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Still a student.
    Location:
    Karachi
    Ok thanks. Your information helped me a lot. Let me thank you once more.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice