Reputation: 45
I'm making a game that needs to appear random phrases on the screen, but they are constantly repeating.
How can I make them appear randomly with no repetitions?
Here is the code:
{
wordList[0] = "Sou Mexicano";
wordList[1] = "Ajuda-me";
wordList[2] = "Salva-me";
wordList[3] = "Vamos Ganhar";
wordList[4] = "Estou sem palavras";
wordList[5] = "Amo Esparguete";
wordList[6] = "Criei o Mundo";
wordList[7] = "Sou Cientista";
wordList[8] = "Carrega o Telemovel";
wordList[9] = "Gosto de Cebolas";
wordList[10] = "Torre de Pisa";
wordList[11] = "Volta a Portugal";
wordList[12] = "Vai Nadar";
wordList[13] = "Andar de Bike";
wordList[14] = "Cortar a Relva";
wordList[15] = "Acender a Luz";
wordList[16] = "Gosto de Animes";
wordList[17] = "Estou Gravida";
wordList[18] = "Nao temos Tempo";
wordList[19] = "Vou tomar Duche";
wordList[20] = "Trabalho em Casa";
wordList[21] = "Gosto de Cenouras";
wordList[22] = "Vamos para a Piscina";
wordList[23] = "Tenho um Cao";
wordList[24] = "Quero Voar";
wordList[25] = "Eles vao passear";
wordList[26] = "Quero Fama";
wordList[27] = "Folha Vermelha";
wordList[28] = "Sai do Facebook";
wordList[29] = "Mete no Instagram";
wordList[30] = "Jantar esta pronto";
wordList[31] = "Temos que conversar";
wordList[32] = "Amanha temos Aulas";
wordList[33] = "Bateria Fraca";
wordList[34] = "Tenho comida";
wordList[35] = "Traga a Conta";
wordList[36] = "Sexta vai ser Feriado";
wordList[37] = "Feito em China";
wordList[38] = "Eu confio nele";
wordList[39] = "Vai ao Skype";
wordList[40] = "Eu amo-te";
wordList[41] = "Liga a Televisao";
wordList[42] = "Olha para ele";
wordList[43] = "Segue-me";
wordList[44] = "Comecem a Contagem";
wordList[45] = "Anda para aqui";
wordList[46] = "Liga-me";
wordList[47] = "Abre o Guarda-Sol";
wordList[48] = "Pare de Insistir";
wordList[49] = "Espera um Bocado";
wordList[50] = "Bom Dia";
wordList[51] = "Tenho boas Noticias";
wordList[52] = "Vamos Terminar";
wordList[53] = "Fumar Mata";
wordList[54] = "Vais Preso";
wordList[55] = "Que Calor";
wordList[56] = "Que Frio";
wordList[57] = "Vou-me Vestir";
wordList[58] = "Eles Pagam";
wordList[59] = "Devolve-me a Carteira";
wordList[60] = "Vai Anoitecer";
wordList[61] = "Vamos fazer Direta";
wordList[62] = "Acabou o Whisky";
wordList[63] = "Vamos jogar LoL";
wordList[64] = "Nao Percebi";
wordList[65] = "Vamos Beber";
wordList[66] = "Porque me Abandonaste";
wordList[67] = "Estou Casado";
wordList[68] = "Acesso Proibido";
wordList[69] = "Mete na Caixa";
wordList[70] = "Fecha a Porta";
wordList[71] = "Tranca o Cofre";
wordList[72] = "Abre a Garagem";
wordList[73] = "Vamos ao Cinema";
wordList[74] = "Queres namorar Comigo";
wordList[75] = "Vou ao Continente";
wordList[76] = "Estou no Jardim";
wordList[77] = "Fui lavar o Carro";
wordList[78] = "Eles jogam Andebol";
wordList[79] = "Vou andar de Skate";
wordList[80] = "Queres jogar FIFA";
wordList[81] = "Vamos ter Teste";
wordList[82] = "Procura na Mochila";
wordList[83] = "Falei com o Diretor";
wordList[84] = "Demorou muito Tempo";
wordList[85] = "Nao gosto de Abacaxi";
wordList[86] = "Viste o Acidente";
wordList[87] = "Caixa Azul";
wordList[88] = "Boneco de Neve";
wordList[89] = "Eu Conduzo";
wordList[90] = "Nao bebas muito";
wordList[91] = "Flor Amarela";
wordList[92] = "Saco Preto";
wordList[93] = "Faz o Comer";
wordList[94] = "Lembra-te de Comer";
wordList[95] = "Onde Estou";
wordList[96] = "Mete ao Contrario";
wordList[97] = "Trabalho num Apartamento";
wordList[98] = "Nao ouvi nada";
wordList[99] = "Estou no Limite";
}
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(100);
String wordToDisplay = wordList[randomInt];
TextView frase = (TextView) findViewById(R.id.textView7);
frase.setText(wordToDisplay);
Sorry if this is a duplicate question but I already searched and didn't find the solution for this one.
Upvotes: 0
Views: 68
Reputation: 45
I was able to find a method that makes the words repeat fewer times. Here's the code:
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(100);
String wordToDisplay = wordList[randomInt];
TextView frase = (TextView) findViewById(R.id.textView7);
frase.setText(wordToDisplay);
List<String> valores = new ArrayList<String>();
valores.add(wordToDisplay);
Collections.shuffle(valores);
Upvotes: 0
Reputation: 15685
Make a copy of your list. Shuffle the copy. Pick the phrases off the list in the shuffled order until they are all used. If necessary make another copy and shuffle it. You will get the same phrases, but in a different order. Java has Collections.shuffle(), I am not sure about Android.
Upvotes: 2