vendredi 25 septembre 2015

Non repetitive random number generator in c

I want to make a program that will give me 4 random numbers in the range 1 - 20 without any of them being the same. It does give me 4 different random numbers but every couple of tries 2 numbers are the same. I don't want that. Here's my code:

int main(){
int g;
srand(time(0));
start:;
scanf("%d",&g);
switch(g){
case 1:RNG_4_10();
break;
default:exit(0);
break;
}
goto start;
}

int RNG_4_10(){

int a,n,i,c;
for(c=0;c<10;c++){
printf("\n");
for(i=0;i<4;i++){
        a = (rand() % 20 + 1);      //give a random value to a;
        n = a;                      //assign n the value of a;
        while(a == n){
        a = rand() % 20 + 1;                  
        }
        printf("%d\t",a);
}
}
}




Aucun commentaire:

Enregistrer un commentaire