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...

Recommended Answers

All 5 Replies

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.