I use rand() and i don' t see the reason for using the <time.h> library...does anyone know?
I use rand() and i don' t see the reason for using the <time.h> library...does anyone know?
Because one way to seed the pseudorandom number generator is to use the current time, or a hash thereof. Read Prelude's article on using rand().
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I know how to use rand, srand but i do not include <time.h> and still the output are radom numbers...why? i mean why then to use it if we already have what we want...i hope you understood what i am asking...
There are other ways to seed the pseudorandom number generator. If you do not explicitly seed it, it will be as if you called srand(1).Originally Posted by brack
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Oh, so that is why it returns random numbers again...thank you!
One more question, is there anyway that i can print the real time(that clock says while i run the program)?
What exactly do you mean by that? You might want to start a new thread concerning this new problem.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
i have done many programs like that and i don' t seem to agree...
srand gives the seed number...so all the others are not the same...maybe i misunderstood what you said...
I'm sure this is compiler dependent, but in most of the C compilers I've used "srand(99999)" for example followed by a series of printf(rand() %10000) has always printed out exactly the same sequence every time.
By using time as your seed you get a different seed and thus a different sequence, every second.
before srand i was using scanf so every time there were a different number...never mind you are correct !
If you don't initialize the seed, or initialize it with the same number all the time, you will find that you will get the same sequence of numbers all the time!
That's because a random number generator is not really so random. All it does is transform the seed using some algorithm into a new number. And then it uses that random number as the new seed to generate the next number, and so on. Since algorithms are deterministic, it will always produce the same output for any given number.
Therefore, to get actual "random" numbers, you have to seed it with a unique seed everytime you run the program. The easiest way to do so is to use the current time.