[Tutor] Filling an array - with a twist

Phil phillor9 at gmail.com
Wed Nov 11 01:30:54 EST 2020


Another call on the Brains Trust I'm afraid.

I'd like to fill an 8 x 8 array (list of lists) with random numbers such 
that no column or row repeats the same number more than twice in a sequence.

e.g.. [12344562] is OK but [12223456] is not wanted.

Something like the following will give me a row or column that meets the 
requirements but more often than not fails the complete array test.

 >>> import random
 >>> r = random.sample(range(0,8),8)
 >>> r
[6, 2, 4, 5, 3, 0, 1, 7]

Searching through the array trying to repair an errant sequence is not 
the answer. Can anyone suggest an algorithm to do what I need?

To add to the complication I really need numbers between 0 and 6 which 
random.sample() won't allow because, as the value error states, the 
sample is larger than the population.

-- 
Regards,
Phil



More information about the Tutor mailing list