[Solved] What does Collections.shuffle function do


I don’t think you’re really asking about what Collections.shuffle does in general: I think you’re asking why Collections.shuffle affects the output:

You are creating a load of tasks of two kinds: one of them increments a value (“kind 1”); the other increments a value and sometimes prints a message (“kind 2”). These are put into a list, and executed sequentially.

If you put an equal number of these two kinds of tasks into a list, shuffle them, and execute them sequentially, the item at a position is going to be a “kind 1” task 50% of the time, and a “kind 2” task the rest of the time.

Since “kind 2” tasks only prints for a specific value, you will only get something printed if it is a “kind 2” task at position 2 * size - 1, which will be 50% of the time.

If you don’t shuffle them, but add “kind 1” and “kind 2” in turn, the item at position 2 * size - 1 will always be a “kind 2”, so it will always print.

0

solved What does Collections.shuffle function do