user1813867
user1813867

Reputation: 1055

Generate a new numpy array of order 2 filling in each element with a random number in a certain range

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.

Upvotes: 2

Views: 688

Answers (1)

arshajii
arshajii

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

Related Questions