Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- from random import randint
- def genRandomMatrix():
- matrix = np.zeros((5,5))
- for ix in range(5):
- for iy in range(5):
- u = randint(1,5) # generate random int to test
- while True:
- if u in matrix[ix][:] or u in matrix[:][iy]:
- # test if random int already in indexed row or indexed column
- u = randint(1,5)
- # if in row/column, try a new random int
- else:
- matrix[ix][iy] = u
- # safe to insert random int
- break
- print matrix
- genRandomMatrix()
- [[3. 0. 0. 0. 0.]
- [0. 0. 0. 0. 0.]
- [0. 0. 0. 0. 0.]
- [0. 0. 0. 0. 0.]
- [0. 0. 0. 0. 0.]]
- [[3. 1. 0. 0. 0.]
- [0. 0. 0. 0. 0.]
- [0. 0. 0. 0. 0.]
- [0. 0. 0. 0. 0.]
- [0. 0. 0. 0. 0.]]
- [[3. 1. 2. 0. 0.]
- [0. 0. 0. 0. 0.]
- [0. 0. 0. 0. 0.]
- [0. 0. 0. 0. 0.]
- [0. 0. 0. 0. 0.]]
- [[3. 1. 2. 4. 0.]
- [0. 0. 0. 0. 0.]
- [0. 0. 0. 0. 0.]
- [0. 0. 0. 0. 0.]
- [0. 0. 0. 0. 0.]]
- [[3. 1. 2. 4. 5.]
- [0. 0. 0. 0. 0.]
- [0. 0. 0. 0. 0.]
- [0. 0. 0. 0. 0.]
- [0. 0. 0. 0. 0.]]
Add Comment
Please, Sign In to add comment