Guest User

Untitled

a guest
Apr 27th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. import numpy as np
  2. from random import randint
  3.  
  4. def genRandomMatrix():
  5. matrix = np.zeros((5,5))
  6.  
  7. for ix in range(5):
  8. for iy in range(5):
  9. u = randint(1,5) # generate random int to test
  10. while True:
  11. if u in matrix[ix][:] or u in matrix[:][iy]:
  12. # test if random int already in indexed row or indexed column
  13.  
  14. u = randint(1,5)
  15. # if in row/column, try a new random int
  16.  
  17. else:
  18. matrix[ix][iy] = u
  19. # safe to insert random int
  20.  
  21. break
  22.  
  23. print matrix
  24.  
  25. genRandomMatrix()
  26.  
  27. [[3. 0. 0. 0. 0.]
  28. [0. 0. 0. 0. 0.]
  29. [0. 0. 0. 0. 0.]
  30. [0. 0. 0. 0. 0.]
  31. [0. 0. 0. 0. 0.]]
  32. [[3. 1. 0. 0. 0.]
  33. [0. 0. 0. 0. 0.]
  34. [0. 0. 0. 0. 0.]
  35. [0. 0. 0. 0. 0.]
  36. [0. 0. 0. 0. 0.]]
  37. [[3. 1. 2. 0. 0.]
  38. [0. 0. 0. 0. 0.]
  39. [0. 0. 0. 0. 0.]
  40. [0. 0. 0. 0. 0.]
  41. [0. 0. 0. 0. 0.]]
  42. [[3. 1. 2. 4. 0.]
  43. [0. 0. 0. 0. 0.]
  44. [0. 0. 0. 0. 0.]
  45. [0. 0. 0. 0. 0.]
  46. [0. 0. 0. 0. 0.]]
  47. [[3. 1. 2. 4. 5.]
  48. [0. 0. 0. 0. 0.]
  49. [0. 0. 0. 0. 0.]
  50. [0. 0. 0. 0. 0.]
  51. [0. 0. 0. 0. 0.]]
Add Comment
Please, Sign In to add comment