I use rand() in a look, and it always give me the same values several times in a row. I tried to use srand(time(NULL)) before the loop, but it does not help...
-
8Please show code. – jason Feb 01 '11 at 05:04
-
4[Relevant](http://dilbert.com/strips/comic/2001-10-25/). – dreamlax Feb 01 '11 at 05:07
-
@dreamlax: Exactly what I was thinking. – Matt Joiner Feb 01 '11 at 05:09
-
Please edit your question (precisely and concisely) and don't put additional information just in comments to answers. – Jens Gustedt Feb 01 '11 at 07:56
-
possible duplicate of [Why do I always get the same sequence of random numbers with rand()?](http://stackoverflow.com/questions/1108780/why-do-i-always-get-the-same-sequence-of-random-numbers-with-rand) – πάντα ῥεῖ Mar 08 '15 at 09:14
1 Answers
Usually, the only reason you get repeated numbers is if you use srand
within the loop with the same seed value (and time(0)
will give you the same value in a tight loop).
Of course, a true random number sequence can give you repeated numbers. Even one that doesn't do that can give you the same number repeatedly if you're manipulating it badly.
For example, rand() / 100000
may be a not-so-good thing to do if the algorithm tends to favour changes at the low end of the returned value since the rand()
sequence of 100000, 164534, 186410, 199999
will give you 1, 1, 1, 1
(rand() % 100000
may well be a better choice in that case if changes between consecutive number is what you value).
Of course, without seeing your actual code, guesses like that are probably the best we can do. Your best bit, as with most problem reports, is to provide a small complete sample that exhibits the problem.

- 854,327
- 234
- 1,573
- 1,953
-
-
-
The problem is that I am not getting a lot of 1s. The numbers that I am getting are random, but repeated several times. I also noticed that the first one is always different from others. the second one always repeat at least four times... – user512853 Feb 01 '11 at 05:38