Would that be more random than just calling the time seed alone?Code:srand(time(NULL)); srand(rand() % 5000);
Would that be more random than just calling the time seed alone?Code:srand(time(NULL)); srand(rand() % 5000);
No not really. You will only seed srand with a different number.
I've ran some tests on both. My method seems to be more random. I tested with really small numbers (0-5) small numbers (0-50) and large numbers (0-500)
Seeding with just time had some repeating numbers, my method had the numbers more spread apart.
But that doesn't nececsary mean that the numbers are more random. The series
'1,1,1,1,1' is just as likely as '2,50,765,78,912'
random is random... I've never had a problem with random numbers b4 where i get the same number each time. I use the random seed generator:
with <windows.h> and that always works fine for me.Code:srand(GetTickCount())
Hey, you gotta start somewhere
BASIC's system, once compiled, would always select the same numbers. Its just that you wouldn't have to specify the numbers. Theoretically, using that code shouldn't make much of a difference, but go ahead and try it. Once something is random, it's about as random as it's going to get. It's like if I shoot something, it's dead. I can't shoot it again to make it more dead.
Wouldn't frenchfry's way actually be alot less random? Using the time as a seed will always give you a different seed than before, as time goes forward all the time. But with his, you'd only have 5000 (or whatever number he chooses) different seeds, and thus, it would give out the same numbers every 5000 times you run the program, or so. Ofcourse it might choose the same number more often, but you know what I mean.
rand() is a psuedo-random number generator. It will always produce the same sequence of "random" numbers with a given seed.
srand() take an unsigned int as its argument so the total possible number of seeds is ~65500 for a 2-byte int and ~4,300,000,000 for a 4-byte int (which is more likely now days). Which means, it is possible to have the same seed every now and again but rather unlikely. As pointed out, if you mod by 5000, you have just cut down the total possible seeds to only 5000.
If a tree falls in the forest, and no one is around to see it, do the other trees make fun of it?
Also, you should use RAND_MAX to calculate your random numbers:
(rand() * 5) / RAND_MAX
See this thread for an explanation:
http://www.cprogramming.com/cboard/s...highlight=rand
French, in most cases srand() and rand() provide sufficently random number for an application.
Also, if you WERE to come up with a completely random random number generator, you would most likely get a PhD out of it. The security and encryption industry is always looking for more random random numbers.
how about this (I'm just trying to bug you with this one )
Code:srand(time(NULL)); srand(rand() % time(NULL));
french, I can tell you very simply why that would fail to produce a more random number.
The only unique number given to srand is time(). Therefore, there is an upper limit on randomness set at srand(unsigned(time(NULL)))
Think about it this way:
For any given value returned by time(NULL), or any given set of values returned by multiple calls to time(NULL), the pseudo-random number generator can only be seeded in one manner. Subsequent calls to rand() will return numbers based only on this value or set of values.
If anyone were to write an algorithm that returned truly random numbers, it would not take a seed value. But this is beyond the scope of modern computer science. This is how random numbers tie in with encryption. Due to the ones and zeros nature of a computer, any random pattern of numbers is simply an ordered pattern of numbers whose order has been obfusciated beyond the casual appearance of a pattern.