jeudi 3 novembre 2016

2D array, prototype function and random numbers

A part of my program is supposed to create an array holding either an asterisk or a dot. I have to use a prototype function where if the random number generated(between 0 and 1) is less than a variable p, the array should hold '*'. otherwise it should hold '.'. The array created is only producing dots. What's wrong? (i have declared the function earlier in the program)

else {
for( i = 0; i<m;i++){
    for( j = 0; j<n; j++){
                array[i][j] = assignBomb(p);
                printf("%c", array[i][j]);

    }
    printf("\n");
}

return 0;
}

int assignBomb(double p) {

srand((int)time(0)); //random number seed
double random;
random = ((double) rand() / RAND_MAX); //generates random number betwn 0 & 1
char cellchar; //declaring a character variable

if (random < p) {
    cellchar = '*';
}
else {
    cellchar = '.';
}

return cellchar; //function returns cellchar
}




Aucun commentaire:

Enregistrer un commentaire