lundi 22 octobre 2018

Simple C random lottery number

I would like to generate 5 different numbers in the simplest way and put them into an array. Something is wrong with my way of thinking, could you please correct my code?

void lottery(int *array){
    int i = 0;
    while(i != 5){
        bool good = true;
        int number = rand()%90+1;
        for(int j=0; j<5; j++){
            if(array[j] == number)
                good = false;
                break;

        }
        if(good){
            array[i] == number;
            i = i+1;
        }
    }
}

int main(){
    srand(time(0));
    int numbers[5];
    lottery(numbers);

    for(int i =0; i<5; i++){
        printf("%d, ",numbers[i]);
    }
    return 0;
}




Aucun commentaire:

Enregistrer un commentaire