How to chose only one picture which can’t repeat?
I tried this:
I use random and i want to delete selected picture
String[] picture = { "blue", "white", "red", "yellow" };
int number_picture1;
// Random function to find a random picture
Random ran = new Random();
number_picture1 = ran.nextInt(picture.length);
System.out.println(picture[number_picture1]);
// There is still posibility to chose the same picture
int number_picture2;
number_picture2 = ran.nextInt(picture.length);
System.out.println(picture[number_picture2]);
Simplest way is to use a
List1 to store your elements, and useCollections.shuffle()on it – and then take elements iteratively.The shuffle produces a random permutation of the list, so chosing items iteratively gives you the same probability to chose any ordering, which seems to be exactly what you are after.
Code snap:
(1) Simplest way to get the list from an array is using
Arrays.asList()