Reputation: 1055
I'm new to numpy. What's the best way to create a new array and fill each element with a random number within a certain range?
For example I want a 3-by-3 array where each element is either a 0 or a 1.
0
1
Upvotes: 2
Views: 688
Reputation: 129497
Try something like
np.random.randint(2, size=(3, 3))
See this documentation for further details.
For example:
import numpy as np print np.random.randint(2, size=(3, 3))
Output:
[[1 0 0] [1 1 0] [1 0 0]]
Upvotes: 6