vendredi 15 juillet 2016

Random generated number cannot equal number in array, issue [on hold]

For one of the problems in my book I have an array of ints between 0 - 54. Then I am to generate a random number that cannot equal any of the values in that array. I used a for loop to compare the values in the array to the random number generated. If the random number equals a number in the array it generates another number, until they aren't equal. The issue I am having is that a previous number already checked can still occur when a number after it is checked. How can I get around this issue, or is there another way I should be comparing the numbers. Sorry for the title, a bit confused on how to ask the question.

num = (int)(Math.random() * 55);
for(int k = 0; k < numbers.length; k++) {
    while(num == numbers[k]) {
        num = (int)(Math.random() * 55);
    }

Thank you.




Aucun commentaire:

Enregistrer un commentaire