I was wondering if there was a method to make the time seed for rand increment it's randomness in milliseconds or thousandths of a millisecond as a method to make it actually random. If I run this code as it is I get numbers like 8834,8842,8856 .etc. so unless I'm going to run it then wait an hour or 2 then run it again this code is kinda useless as far as RNG goes. Though I suppose it may work for the dice roller program I learned it from(although still being predictable output) but for any bigger numbers I don't see it being of much use.
rand() is basically a mathematical computation. It runs an input number 'x' through some computations, and results in output 'y'.
The output 'y' becomes the input 'x' for the next call to rand(). So each time you call rand, it is using it's previous output as its new input. Thus it generates a new number each time.
All srand() does is give the computations that first 'x' input -- it gives it a seed... a starting point... from which the computations can start.
Using time() as the seed is effective because it's virtually guaranteed to be unique each time the program starts.. ensuring that the computation will start with different input... generating a different stream of output numbers each time.
second... millisecond... microsecond granularity doesn't really matter. The point is to have a unique seed. The 1 second granularity offered by time() works just fine unless you run the program multiple times within the same 1 second window (in which case each instance of the program will produce the same stream of random numbers).
If you are getting predictable output instead of random output... it's much more likely that you are misusing rand. Can you post some example code that illustrates the problem you're seeing?
in your code, do you call srand() more than once? It should be called just once at the start of execution (usually at the beginning of main).
Also, if you are re-running the program again after only a brief time has elapsed there may be some visible pattern in the initial random number generated. It can help to begin by seeding the generator with srand, then call rand() to generate one or more numbers which are simply discarded, before going on to execute the rest of the program.
> begin by seeding the generator with srand, then call rand() to generate one or more numbers
> which are simply discarded, before going on to execute the rest of the program.
+1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include <cstdlib>
#include <ctime>
void srand_and_warmup( int seed = std::time( nullptr ), int discard = 16 )
{
std::srand( seed ) ;
for( int i = 0 ; i < discard ; ++i ) std::rand() ;
}
int main()
{
srand_and_warmup() ;
std::cout << std::rand() << '\n' ;
}
Yeah I guess it depends on the rand() implementation and the RNG used. I'm surprised that an implementation would be so shoddy, though.
I mean... come on....
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// custom rand()/srand() functions that probably are better than
// some distributions of rand()
namespace
{
unsigned state = 0;
}
void srand(unsigned seed)
{
state = seed;
}
unsigned rand()
{
return state = (69069*state) + 362437;
}