When I generate 5 random numbers, they get placed in the cards array. The number generated goes in the pack array case and replaces it by one. I want to add a condition that if the pack array is 1, to generate another random number. My goal is to generate 5 random numbers that do not repeat themselves. How would I do that ? Thanks
Here is the code :
int[] pack = new int[52];
int[] cards = new int[5];
for (int i = 0; i < pack.length - 1; i++) {
System.out.print(pack[i] + " | "); // pack array with the 52 available cards
}
System.out.println();
Random give = new Random();
for (int i = 0; i < cards.length; i++) {
cards[i] = give.nextInt(5); //generates a random number to place in the cards array
pack[cards[i]] = 1; //the pack case gets the value one meaning the generated number can't be generated again
System.out.println(cards[i]);
}
for (int i = 0; i < pack.length - 1; i++) {
System.out.print(pack[i] + " | "); // pack array : cases with the number 1 mean that the number can't be generated again
}
Aucun commentaire:
Enregistrer un commentaire