I have an issue with the random generator. I use the code;

myNumber = rand() % 100; //for numbers 1 - 100;

My problem is, the numbers getting generated have a pattern. Such that the first output is 33, 43, 62, 29, 0, 8 ...
Why is this so?
I am on Windows 8, Using Netbeans 8, with Cygwin installed.

Thanks in advance...

2 Years
Discussion Span
Last Post by rubberman

Why is this so?

rand is always seeded with 1 by default, so unless you change the seed with srand, the sequence will be consistent.


I would go so far as to say never use rand()

Unless you wrote it, of course. ;) I'm at the point with my C standard library that I use it instead of the built-in standard library for most personal work. rand is implemented as the Mersenne Twister, so I know it's solid at least in terms of the underlying algorithm.

In C++, I agree that there's really no excuse to ignore the <random> library if your compiler conforms sufficiently to C++11.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.