mardi 28 avril 2020

eliminating repeated random numbers

I need to get the numbers 0-11 in a random order and make sure each number is only gotten once. It is still sending same numbers into my Line2 class though. How do I make sure that the int variable lineChoice is completely different every time? My problem is the if/else is not properly making sure that I don't send an already selected lineChoice to my Line2 class.

for(int i = 0; i < 12; i++){
        //get a random line choice 0-11
        lineChoice = (int)(Math.random() * 11); //0-11;
        //if that number was already chosen
        //get a new random number and add it to the array
        if(randomNumbers.contains(lineChoice)){
            lineChoice = (int)(Math.random() * 11); //0-11;
            randomNumbers.add(lineChoice);
        }else{
            //if not already in array, add to array
            randomNumbers.add(lineChoice);
        } 
        //make a Line based on the random lineChoice number
        line = new Line2(lineChoice);
        //add the line to the string poem
        poem += "\n" + line + "\n\n\n";                        
    } 



Aucun commentaire:

Enregistrer un commentaire