0

I created a random number generator (between 100 and 1) for an Array but each time I run it it outputs the same 10 numbers between 1 and 100.

for (int x=0; x < 10; x++) {
       arr[x]=rand() % 100 + 1;

The Loop runs 10 times because the array contains 10 integers. Every time I run this though, it outputs: 42, 68, 35, 1, 70, 25, 79, 59, 63, 65.

It Outputs from this line of code

 for (int z=0; z<10;z++) {
       cout << arr[z]<<", ";
 }

Can anyone see why this is happening?

jerry
  • 1

1 Answers1

0

When using rand() it will generate the same sequence from the given input to srand(). Another words it will typically use the same generated numbers from the initial seed value. These functions are considered to be frowned upon in their use and some even want them to be marked as deprecated in favor of the modern std pseudo-random number generators library.

To generate random numbers try using the library from the <random> header instead.

You can find the documentations here: cppreference::random

Francis Cugler
  • 7,788
  • 2
  • 28
  • 59