dimanche 2 décembre 2018

Array with random values without repetition in C

I need to generate an array with random values, however the values should be between 1 and numOfCities without repetition of values. Another problem that i'm having is when I call this function multiple times it returns always the same array.

That's what I did so far:

int * getRandom(int numOfCities) {

    int * randomArr = malloc(sizeof (int) * numOfCities);
    randomArr[numOfCities];

    srand((unsigned) time(NULL));
    int var = 0;

    for (int i = 0; i < numOfCities; ++i) {
        var = rand() % (numOfCities + 1);
        randomArr[i] = var;
     }
    printf("\n");
    return randomArr;
}




Aucun commentaire:

Enregistrer un commentaire