Following your question, I wrote the following program. I created a list of consecutive integers and shuffled it 10, 100, 1000, and 10,000 times. After each series of tasses, I checked the value of the element in the 5th position of the array and created an array of counters: how many times each number appears in the 5th position.
Here is the program:
public class MyTest { public static void main(String[] args) { int n = 10; List<Integer> list = new ArrayList<Integer>(); for (int i = 0; i < n; i++) { list.add(i); } int[] counters = new int[n]; for(int shuffles : new int[] {10, 100, 1000, 10000}) { Arrays.fill(counters, 0); for (int i = 0; i < shuffles; i++) { Collections.shuffle(list);
And here are the results:
10: [0, 1, 1, 1, 2, 0, 0, 3, 2, 0] 100: [11, 9, 9, 7, 10, 12, 13, 13, 8, 8] 1000: [100 , 101, 107, 101, 95, 96, 109, 83, 93, 115] 10000: [1015, 942, 990, 1003, 1015, 1037, 977, 1060, 950, 1011]
As you can see, "randomness" depends on the amount of shuffling. If you shuffle the array 10 times, the minimum counter is 0, and the maximum is 3. The difference between these values โโfor 100 shuffles (in percent) is much smaller. The numbers are almost the same for a 10,000 shuffle.
I think this test simulates your use case: you display images at a specific position in a shuffled collection.
Please see the @amit post which describes the meaning of shuffling.
So, the solution for you is to shuffle your array 10 times.
EDIT: @Dave Webb gave a great explanation for this case.
The second thinking is this: in fact, you donโt need to shuffle the list of 1000 elements in order to extract the first 20 elements from it. It is enough to take 20 random elements. You will get the same effect, but a much more effective solution:
Set<Image> show = new HashSet<Image>(); Random r = new Random(System.currentTimeMillis()); for (int i = 0; show.size() < 20; i++) { show.add(list.get(r.nextInt())); }