vendredi 7 septembre 2018

Creating an array filled with random unique numbers

So i'm trying to create an array of 4 items that is randomly filled with 4 unique numbers. Currently I have a hugely long while loop which checks each value in the array and keeps repeating if they are the same. It's really badly coded and I was wondering if you guys could help me figure out a better way? Here's what I currently have.

int array[4];
while(array[0] == array[1] || array[1] == array[2] ||  array[2] == array[3] || array[0] == array[2] || array[0] == array[3] || array[1] == array[3])
{
    array[0] = rand() % 20;
    array[1] = rand() % 20;
    array[2] = rand() % 20;
    array[3] = rand() % 20;
}




Aucun commentaire:

Enregistrer un commentaire