-1

Why does this result in the same array every time i run it?

int arr[100];
for (int i = 0; i < 100; i++) {
    arr[i] = rand() % 11 + (-5); 
}
BenW
  • 737
  • 10
  • 41

2 Answers2

2

You need to seed the random number generator, for example with the current time.

#include <time.h>
#include <stdlib.h>
srand(time(NULL));
Aganju
  • 6,295
  • 1
  • 12
  • 23
1

Use std::srand(std::time(0)); to set the random seed. For reproducible results, use the same seed, see http://en.cppreference.com/w/cpp/numeric/random/srand.

David Stutz
  • 2,578
  • 1
  • 17
  • 25