0

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

4
Contributors
5
Replies
17
Views
1 Year
Discussion Span
Last Post by rubberman
Featured Replies
  • 1

    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 using `rand()` to reduce the chance of ever using it accidentally). Read More

  • 1

    > 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 … Read More

  • 1

    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 Read More

0

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.

1

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.