Jake Jackson
Jake Jackson

Reputation: 1225

How to generate N-dimensional array of random values?

Currently, I am writing this:

    x, y = np.array([random.randint(0, 9), random.randint(0, 9), random.randint(0, 9)]), \
           np.array([random.randint(0, 9), random.randint(0, 9), random.randint(0, 9)])

Is there a built-in NumPy function to allow me to do this a bit better?

I said N-dimensional array not because I want to generate a random-sized vector with random values, but rather so others can apply it for their own vectors.

Upvotes: 2

Views: 1458

Answers (1)

Thomas Schillaci
Thomas Schillaci

Reputation: 2453

You can use:

import numpy as np
np.random.randint(0, 9, shape)

For example, with shape=(2, 3):

array([[3, 2, 0], [1, 0, 1]])

Upvotes: 4

Related Questions