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

I would be remiss if I did not point you at this:

https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful

I would go so far as to say never use rand(), even in toy programs just for fun (because you might as well practice not …

Jump to Post

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, …

Jump to Post

Read this tutorial on random number generators and you will understand better how all this works: http://www.phy.ornl.gov/csep/CSEP/RN/RN.html

Jump to Post

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.