Xankar Xankar - 4 years ago 160
C++ Question

Fill a 2D array with non-repeating random numbers

I am currently trying to fill up a 2D array with 16 values from 1-16. I want to fill up the array with non-repeating random values.

For example, if I have an array

int array[4][4]
how would I go through a loop filling the array while checking to make sure that another identical random value isn't placed into another location in the array?

Answer Source
int array[4][4];
int* p = &array[0][0];
std::iota(p, p+16, 1);

std::random_device r;
std::default_random_engine g(r());
std::shuffle(p, p+16, g);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download