jeudi 28 avril 2016

C: create random numbers from a range that are all different

For exemple i've this 3 variables:

N = 5;
int r1 = rand() % N;
int r2 = rand() % N;
int r3 = rand() % N;

How can i do have 3 different numbers in that range and not the same? I tryed with this code:

do{
  int r1 = rand() % N;
  int r2 = rand() % N;
  int r3 = rand() % N;
}while((r1 == r2) && (r1==r3) && (r2==r3));

but it doesn't works. Sometimes one or two number are the same (ex: 1, 1, 4). But i need all different. How can i do? Thank you




Aucun commentaire:

Enregistrer un commentaire