Thread: Newbie question about srand

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    13

    Newbie question about srand

    I am new to C++ and still trying to understand the nuances of the the syntax. In the below example i am using the srand() function to set my random seed. I am just wondering why srand needs to be in the main part of my program and cannot be outside in the front end of the program?

    thank you for your time

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    using namespace std;
    int seed_storage = time( NULL ); //set seed_storage to random seed value
    int randRange(int low, int high)
    {
        return rand()%(high+1-low)+low; //take the modulus of the generated number with respect to the difference between the high and low values of the range then add low.
    }
    int hiddenNumber; //value to hold random generated number
    int main()
    {
    srand(seed_storage); //use seed_storage value as seed for rand() function
    while(true)
    {
        hiddenNumber = randRange(0,100);
    }
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Mathieas
    I am just wondering why srand needs to be in the main part of my program and cannot be outside in the front end of the program?
    Because it is a function call.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    13
    So function calls can only be present within the main function of the program, that makes sense. Thank you

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, not quite. You can have function calls at file scope where they are used to initialise a global variable, but... why are you using global variables?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    13
    I am using global variables mostly because i am teaching myself and as a result I prob have bad habits(I am using the jumping into c++ online book). I was just reading global variables are not the way to go in most cases. I have used them for so long that i have become used to using them...

    I am not sure i understood your 2nd response. Could you give an example?

    Thank you again for your time, I still dont have a good sense on how to ask the proper questions and often times i lack the vocab needed to do a useful search.
    Last edited by Mathieas; 06-01-2012 at 12:07 AM.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    One example would be if you changed line 10 to:
    Code:
    int hiddenNumber = randRange(1, 42);
    Don't do that by the way, as it would get executed before the srand call and thus not end up random at all.

    Edit: Actually your line 5 is already an example of that.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    13
    Quote Originally Posted by iMalc View Post
    One example would be if you changed line 10 to:
    Code:
    int hiddenNumber = randRange(1, 42);
    Don't do that by the way, as it would get executed before the srand call and thus not end up random at all.

    Edit: Actually your line 5 is already an example of that.
    Thank you for the reply. That makes sense however I am not quite clear why I call a function to int a variable but cannot call a function straight up, as in the srand case. What is the logic behind syntax?

    thank you both for your time and expertise,

    Jonathan

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Mathieas
    That makes sense however I am not quite clear why I call a function to int a variable but cannot call a function straight up, as in the srand case. What is the logic behind syntax?
    Well, if you just examine it at one level (at file scope), a program is a sequence of declarations. A function prototype is a declaration of that function; a function definition is also a declaration of that function. However, a function call is not a declaration of that function, hence you cannot place it at file scope. But within the syntax of the initialisation of a variable, it is okay, since that syntax is syntax to define the variable, and a definition of a variable is a declaration of that variable.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Apr 2012
    Posts
    13
    Quote Originally Posted by laserlight View Post
    Well, if you just examine it at one level (at file scope), a program is a sequence of declarations. A function prototype is a declaration of that function; a function definition is also a declaration of that function. However, a function call is not a declaration of that function, hence you cannot place it at file scope. But within the syntax of the initialisation of a variable, it is okay, since that syntax is syntax to define the variable, and a definition of a variable is a declaration of that variable.
    Laser,

    Thank you, it is comming together slowly. So if i understand, function calls are allowed in the but not at the file scope level becuase that portion of the program is for declarations only. Another example to clairify, I could call a function within a function definition whether i am in the Main function or not becuase i am declareing that function is this correct?
    Thank you again,

    Jonathan

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Mathieas
    Another example to clairify, I could call a function within a function definition whether i am in the Main function or not becuase i am declareing that function is this correct?
    Yes. The body of a function definition is basically a compound statement, i.e., it contains a sequence of statements. Hence, a statement comprising of a function call is fine.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about rand and srand
    By Mathieas in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2012, 09:32 PM
  2. srand() question
    By Beowolf in forum C++ Programming
    Replies: 10
    Last Post: 11-30-2007, 02:53 PM
  3. rand() and srand() question
    By the_winky_files in forum C Programming
    Replies: 4
    Last Post: 09-21-2005, 10:43 AM
  4. srand() question
    By GamingMarvel in forum C++ Programming
    Replies: 7
    Last Post: 01-10-2005, 09:21 AM
  5. srand question
    By abrege in forum C++ Programming
    Replies: 5
    Last Post: 02-12-2003, 09:50 PM