3
I have a doubt in generating numbers without repetition.
I can already generate random numbers from 1 to 8 which is my goal. The catch is that it generates repeated numbers (duplicated, triplicate or even more). In my program there can be no repeated number. My code so far:
for (int k = 1; k < (gameBtn.length / 2) + 1; k++) // 1 até 8
{
int z= 8 + (int)(Math.random() * (1 - 8)); // Máximo 8, mínimo 1
gameList.add(z); // Imprime os números de 1 a 8, podendo sair repetidos
}
Which means I want to make one if (numeros_anteriores_que_sairam != novo numero). If you confirm, print gameList.add(z), otherwise it doesn’t print the number that came out, (or it eliminates equal numbers) or does anything else without printing the number. Every number has to be different!
(gameBtn.length / 2) + 1is 8?– Victor Stafusa
gameBtn.lenght is 16 , so 16/2=8, if sum one gets 9,
– insyspower
did k=1 up to 9, but could have done k=0 up to 8
– insyspower
Do you want from 1 to 8, from 0 to 8 or from 1 to 9? Anyway, I think my answer serves, if only to add a 0 or a 9 to it.
– Victor Stafusa