srand in c or c++ doesn't really Generate Random Numbers ?
-
Hi All I am using the srand function generate random numbers.Here is the problem. for example: #include #include int main() { int i = 0,j = 0; srand((int)time(0)); for(i=0; i<10; i++) { j=1+(int)(10.0*rand()/(RAND_MAX+1.0)); std::cout << j << '\t'; } std::cout << std::endl; return 0; } result: 5 8 8 8 10 2 10 8 9 9 2 9 7 4 10 3 2 10 8 7 The problem is that the two lines of the results are the same when the compiler run over in one second. How the function works? how the computer make the rand numbers? Does the function can generate really Random Numbers ? Please help me out if you find some solution for the above problem. Thanks in advancs ! Where did the good times go? Don't try it, just do it! *Archibald*rever dragon!
-
Hi All I am using the srand function generate random numbers.Here is the problem. for example: #include #include int main() { int i = 0,j = 0; srand((int)time(0)); for(i=0; i<10; i++) { j=1+(int)(10.0*rand()/(RAND_MAX+1.0)); std::cout << j << '\t'; } std::cout << std::endl; return 0; } result: 5 8 8 8 10 2 10 8 9 9 2 9 7 4 10 3 2 10 8 7 The problem is that the two lines of the results are the same when the compiler run over in one second. How the function works? how the computer make the rand numbers? Does the function can generate really Random Numbers ? Please help me out if you find some solution for the above problem. Thanks in advancs ! Where did the good times go? Don't try it, just do it! *Archibald*rever dragon!
Random numbers are very rarely random. They are defined using standard algorithms, and will return the same values based on the same seed value. Hence, they are known as pseudo-random numbers. If you need the sequences to differ, then you need to use a different seed value.
Please visit http://www.readytogiveup.com/ and do something special today. Deja View - the feeling that you've seen this post before.
-
Random numbers are very rarely random. They are defined using standard algorithms, and will return the same values based on the same seed value. Hence, they are known as pseudo-random numbers. If you need the sequences to differ, then you need to use a different seed value.
Please visit http://www.readytogiveup.com/ and do something special today. Deja View - the feeling that you've seen this post before.
thx a lot now i see! Where did the good times go? Don't try it, just do it! *Archibald*rever dragon!