Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[SOLVED] C++: srand(time(NULL)); and rand()........

c++ srand(time(null)); time srand rand null

  • Please log in to reply
3 replies to this topic

#1 BKTheRussian

BKTheRussian

    CC Regular

  • Member
  • PipPipPip
  • 36 posts
  • Location:So. California, U.S.
  • Programming Language:Python
  • Learning:C, C++, Assembly

Posted 13 October 2012 - 06:17 PM

Hey guys, I was wondering if someone can explain to me in a simpler way the function of srand(time(NULL)); in use with rand().

I've been reading and researching and from what I understand is that rand() uses some sort of algorithm that requires its seed to be initialized by some sort of value... so, srand(time(NULL)); uses the system clock, which changes every second, to provide that number. Without using srand(time(NULL)); all of my random numbers came out to be exact same thing over and over. Now.... what is NULL? Is it just a command to reset the value every time the program restarts? Why do I even need to use it... why doesn't rand() just do the random thing every time on its own?

I can just use it without getting too much into it, but I forget things easily or don't use them much if I don't understand them fully.

This is my little guess game I did for the Google C++ exercise. It works... I just want to understand everything I've done fully. Most of the resources on this subject sound like they written by lawyers.

// BK: Guess the number game.
#include <iostream>
#include <time.h>
using namespace std;
int main()
{
// Define two variables: random_number is the random number chosen
// by the program and guessed_number is the user input.
int random_number, guessed_number;

// Initialize random seed.
srand(time(NULL));

// Generate random number between 1 and 100
random_number = rand() % 100 + 1;

cout << "Guess the number (1 to 100): ";
do {
	 if (!(cin >> guessed_number)){
	 cout << "Please enter only numbs."
		 << endl;
		 } else {
			 if (random_number < guessed_number){
cout << "The secret number is lower."
					 << endl;
} else if (random_number > guessed_number){
				 cout << "The secret number is higher."
					 << endl;
				 }
			 }
	 } while (random_number != guessed_number);
	
cout << "Congratulations! You've guessed the right number ("
		 << random_number << ")."
		 << endl;
system("PAUSE");
return 0;
}

Also, an odd thing I've noticed is that when I removed #include <time.h> all together, the program functioned the same..... Why?
  • 0

#2 RhymeTime

RhymeTime

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 65 posts
  • Location:Odenton, MD
  • Programming Language:C, PHP, Python, JavaScript, Assembly, Others
  • Learning:C, PHP, Python, JavaScript, Assembly, Others

Posted 14 October 2012 - 07:49 AM

If you haven't looked at the man pages for rand() and srand() I recommend you do that. They will provide detail on how they work. From my understanding, is that rand() only produces a pseudo random number, so if you do not provide it with a seed value (or provide it with the same seed value) then it makes sense to me that it would produce the same "random" value. Again this value is not really random as you would normally think of a random value.

NULL is a value that is defined to make life easier for programmers. Typically it is the same as 0 or the address 0x00000000 depending on its use. NULL == 0 evaluates to true. It can mean different things in different situations, read the man page on time() and it will tell you what it does with NULL as a parameter.

As for leaving out the #include. Your compiler will guess that the function time() belongs to the time library and will include it automatically. However it should be throwing a warning saying something about the implicit declaration of time(). This is the compiler telling you that you forgot to include a library that the compiler needs. By default it will include the standard C library time library but if you wanted to use a different time() function then you would have needed to include that library separately. This section explains it a little better:

http://www.physics.d...C_basics/#first

  • 0

#3 neil

neil

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 373 posts
  • Location:Philadelphia
  • Programming Language:C++, Perl, Bash
  • Learning:C, Java, C++, Python, JavaScript, Perl, Bash, Others

Posted 14 October 2012 - 10:20 AM

Some compilers like VC++ don't mind it if you don't include some headers. They auto include it at compile/debug time. (Awful)
But it's always better to inclue them. Not everyone uses the same compiler you do.
  • 0

#4 BKTheRussian

BKTheRussian

    CC Regular

  • Member
  • PipPipPip
  • 36 posts
  • Location:So. California, U.S.
  • Programming Language:Python
  • Learning:C, C++, Assembly

Posted 14 October 2012 - 07:19 PM

K. Thanks guys.
  • 0





Also tagged with one or more of these keywords: c++, srand(time(null));, time, srand, rand, null

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download