samedi 21 juillet 2018

How can I use the rand() function to generate a different number that hasn't been generated before?

// What I mean by this is shown by my example:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int i;
int a;


for (a = 0;a <10;a ++) {
    i = (rand()%10)+1; // generates a number from 1-10
    printf("%d\n", i);
}

// I would like for the loop to generate a number that gives a number that was not generated before. For example, an output such as:

1,3,6,2,8,9,4,10,5,7

instead of:

3,9,10,3,7,9,2,7,10,1

In other words, I would like no copies.




Aucun commentaire:

Enregistrer un commentaire