4x4 Matrix in Python -


im trying code 4x4 matrix in python random integers 1-4. thats easy enough problem want each row , each column 1 time uses of each digit 1-4

example

1 2 3 4

2 3 4 1

3 4 1 2

4 1 2 3

my code 33% of time in loop there happens somthing this

2 1 4 3

3 4 2 1

1 3 x <-------- because of programm cant contiune , end in infinity loop helb how can out? code below

""" programm playing game skyline """ random import randrange  row1 = []  row2 = [] row3 = [] row4 = [] allrows = [row1, row2, row3, row4]  column1 = [] column2 = [] column3 = [] column4 = [] allcolumns = [column1, column2, column3, column4]  def board():     in range(4):         j = 0         while len(allrows[i]) != 4:             x = randrange(1,5)             print(i, j)             if x not in allrows[i] , x not in allcolumns[j]:                  allrows[i].append(x)                 allcolumns[j].append(x)                 j += 1              else:                 continue  board() 

you seem looking permutations, , here how them:

from itertools import permutations = list(permutations([1,2,3,4])) 

now random 4 lists:

import random  itertools import permutations = list(permutations([1,2,3,4]))  _ in range(4):     print a[random.randint(0,len(a)-1)] 

edit 1 looking for:

import random import numpy np  itertools import permutations = list(permutations([1,2,3,4]))  = 0 result = [a[random.randint(0,len(a)-1)]] a.remove(result[0]) print result while < 3:      b = a[random.randint(0,len(a)-1)]      if not any([any(np.equal(b,x)) x in result]):          result.append(b)          +=1      a.remove(b)  print result 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -