简体   繁体   中英

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.

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]])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM