vendredi 23 octobre 2015

Numpy array item order - equal distribution of sequences

Lets say I have a shuffled numpy array:

a = np.array([1,2,3,4,5,6]*6)
np.random.shuffle(a)

How do I ensure that each item in the shuffled array follows each other item an equal number of times?

For example, I want to ensure that the number 1 follows the number 2 in the array the same number of times as it follows the number 4. Likewise for all other numbers

We can assume that the list is circular for this problem, i.e., the first item follows the last

Normally I'd post some code of what I've tried but I'm at a loss when it comes to this.

The most inefficient way I can think of is to write a function that counts how many times a number follows another number, and then to check that all counts are equal. If not, reshuffle.

But this doesn't guarantee that I'll ever end up with a list that meets the equal distribution criteria.




Aucun commentaire:

Enregistrer un commentaire