Thread: srand function question

  1. #1
    Registered User keyboard's Avatar
    Join Date
    Nov 2011
    Posts
    8

    srand function question

    I am studying from a book that was written in 2008. In the book they use srand like this:

    Code:
    #include <stdio.h>
    
    main()
    {
    
    int var = 0;
    
    srand(time());
    
    rand(time() % 10 + 1;
    ...
    Non of that would run on my machine. I had to write it like this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    main()
    {
    
      int var = 0;
    
      srand(time(NULL));
    
      rand(time() % 10 + 1;
    ...
    1. Why did I have to add the extra header files #include <stdlib.h>
    and #include <time.h>?
    2. Why did I have to add NULL as an argument to the rand function?
    3. Why is 'rand(time(NULL))' not written with the 's' like this: 'srand(time(NULL))'?

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by keyboard View Post
    Code:
      rand(time() % 10 + 1;
    Above likely type setting bug.

    Likely they wanted this; to get a random value from 1 to 10.

    Tim S.

    Code:
      var = rand() % 10 + 1;
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User keyboard's Avatar
    Join Date
    Nov 2011
    Posts
    8
    That was my fault for not adding the "var =" to "rand() % 10 + 1;" but what about the other things?

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by keyboard View Post
    I am studying from a book that was written in 2008.
    I think you should throw away the book
    1. Why did I have to add the extra header files #include <stdlib.h>
    and #include <time.h>?
    Because "srand()" and "rand()" are defined in <stdlib.h> and "time()" is defined in <time.h>. You have to tell the compiler where it will find definitions of functions/variables if you don't define them yourself.

    2. Why did I have to add NULL as an argument to the rand function?
    Did you mean time(NULL)?
    time() returns the current time. If you call it with a time_t pointer it also stores this time at that address, if you call it with NULL it just returns the current time.

    3. Why is 'rand(time(NULL))' not written with the 's' like this: 'srand(time(NULL))'?
    The two are different functions: srand() initializes the pseudorandom-number generator and rand() returns one random number generated by the generator.

    Bye, Andreas
    Last edited by AndiPersti; 06-30-2012 at 11:24 AM. Reason: wrong answer to second question

  5. #5
    Registered User keyboard's Avatar
    Join Date
    Nov 2011
    Posts
    8
    AndiPersti: Thanks. That all makes sense to me. The book I am reading is garbage. I'll get another one. Any recommendations?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    By the way, AndiPersti made a typographical error in the second option for declaring main. It should be:
    Code:
    int main(int argc, char *argv[])
    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

  7. #7
    Registered User keyboard's Avatar
    Join Date
    Nov 2011
    Posts
    8
    Thanks laserlight!

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    @laserlight: Sorry, I've edited my post because I wasn't really sure what the OP meant. Thanks anyway for the correction :-)

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by keyboard View Post
    I'll get another one. Any recommendations?
    I personally like "C Programming: A Modern Approach" by K.N.King

    Here is a whole thread about book recommendations.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-14-2011, 08:08 AM
  2. Use srand() twice in a function?
    By Jake.c in forum C Programming
    Replies: 5
    Last Post: 01-21-2009, 12:51 PM
  3. srand() question
    By Beowolf in forum C++ Programming
    Replies: 10
    Last Post: 11-30-2007, 02:53 PM
  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

Tags for this Thread