I completely confuse using rand(). Its generating same number, even using srand()...
Is there something like Math.random() in C?
I completely confuse using rand(). Its generating same number, even using srand()...
Is there something like Math.random() in C?
rand() generates a seemingly non-related sequence of numbers initialized by srand()
So for the same srand() you are probably getting the same sequence of numbers. Or at least the same first number.
Use something like srand(time(NULL), or something similar so also the initialization depends on something random and changing.
Last edited by C_ntua; 06-20-2008 at 02:27 PM.
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I suggest that you read Prelude's articles on:
Using rand()
Random Numbers
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
you have to initialize it the proper way:
should generate 10 different numbers. Well the chance of having rand() return the same value ten times in a row is extremely lowCode:#include <stdio.h> #include <time.h> int main(void) { srand(time(NULL)); int i; for(i = 0; i < 10; i++) printf("%d\n", rand()); return(0); }
Also, you are already discussing a similar problem in another thread
If you seed it with the same value, it will always return the same sequence.
If you dont seed it , it will always return the same sequence as the unseeded sequence.
If you seed it with a random seed, the sequence repeats after a very short period.
rand() is only pseudo-random.
srand(time(0)) will not work if we execute the application twice (fast)...
Indeed. Have you read the articles that I linked to?srand(time(0)) will not work if we execute the application twice (fast)...
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Correct, since time(0) returns an value which is precise down to seconds, and if you run it twice in a row quickly, you get the same second (and even if it's only one or two more, it may not produce significantly different values).
If you really require that mode of operation (random numbers created each time the application runs and the application can be started several times the same second), then you need to use some other method of seeding. Some sort of high-precision time would be a good choice - but how you get that would be OS and/or compiler/runtime dependant.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
laserlight:
I've tried their codes that provides the same results.
Hohoho... ^_^
I've got the simplest way!
Try: srand(GetCurrentProcessId())
Unfortunately its only in Windows...
EDIT:
Use _getpid() to replace GetCurrentProcessId().
Is it safe to do something extreme like: srand(_getpid() * time(0))?
Last edited by audinue; 06-20-2008 at 02:59 PM.
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
The point that I am getting at is to understand, not simply try out code hand outs.I've tried their codes that provides the same results.
One thing to ask is this: why is your program being executed multiple times in quick succession? If this is a normal mode of operation, then perhaps you should maintain state with a database.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
No, I just want to make a real random number generator...
And I think using srand(time(NULL)) to create random number isn't a wise way, because the accuracy still using seconds (or milliseconds)...
Consider a setup such as the one used for HotBits or the website that shawnt's Quantum Random Bit Generator gets its randomness from.No, I just want to make a real random number generator...
That shows that you are ignorant because you have failed to read the articles pointed out to you. The accuracy (or more accurately: precision) does not matter, unless you are using the generator wrongly. The idea behind a pseudo-random number generator is to seed once (and perhaps again after the sequence is exhausted), then use the sequence generated. It is not to keep on seeding and then using the first number in each sequence.And I think using srand(time(NULL)) to create random number isn't a wise way, because the accuracy still using seconds (or milliseconds)...
EDIT:
If you are talking about the predictability of time(NULL), or that srand(time(NULL)) is not guaranteed to be portable as Prelude pointed out, or that the standard random number generator facility is often not of good quality, then that is another matter. Admittedly, the predictability of time(NULL) is related to its precision in seconds, but the point is that it is rand() that generates the random numbers, with srand() there to "select" a sequence to generate.
Last edited by laserlight; 06-21-2008 at 02:46 AM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)