Let's say I have 5 marbles, 3 are red and 2 are blue.
The marbles of the same color are numbered from 1 to n, where n is the number of marbles of that color.
What I need is way to shuffle the list ['red', 'red', 'red','blue', 'blue'] , where the permutations of the individual marbles does not matter.
E.g
['red1', 'red2', 'red3','blue', 'blue'] and
['red2', 'red1', 'red3','blue', 'blue']
are only one solution.
Because my original problem has a lot more marbles of one color, compared to the others, the normal shuffle of the list results in a high bias for certain orders.
One idea I had would be:
- Start with a new empty list,
- Generate 2 random integers (the first integer to choose a color, the second integer to determine how many marbles to pick of that color)
- Append the picked marbles to the new list
- Repeat until all marbles have been used up
I'm wondering if there are some better ways to solve this, maybe with a more guaranteed unbiased result.
Edit: A small scaled scenario would be 4 different colors with 2 to 5 marbles per color. A realistic scenario would be 20 diff. colors, with between 5 and 50 marbles per color.
random.shuffleimplements) will give unbiased shuffles regardless of how many duplicates there are.