Python Forum
20 x 20 Integer array with random numbers - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: 20 x 20 Integer array with random numbers (/thread-11852.html)



20 x 20 Integer array with random numbers - harryk - Jul-28-2018

How do I make a 20 x 20 array and fill it with random 1s and -1s ? I've tried using A=np.random.randint(-1,1,[20,20])
print(A)
but this includes zeros in the array. So how do I make an array without any zeros?


RE: 20 x 20 Integer array with random numbers - Gribouillis - Jul-28-2018

I suggest A = 2 * np.random.randint(-1, 1, [20, 20]) + 1


RE: 20 x 20 Integer array with random numbers - ichabod801 - Jul-28-2018

Use np.random.choice. Pass it an array of -1 and 1 and the size.


RE: 20 x 20 Integer array with random numbers - harryk - Jul-28-2018

yeah that works thank you very much