Use the size parameter:

import numpy as np
coordinates = np.random.randint(0, 100, size=(30, 10, 2))

will produce a NumPy array with integer values between 0 and 100 and of shape (30, 10, 2).

Answer from unutbu on Stack Overflow
๐ŸŒ
Data Science Dojo
discuss.datasciencedojo.com โ€บ python
Initializing a 3D Numpy array with random values in Python - Python - Data Science Dojo Discussions
January 30, 2023 - 1. Using the np.empty function: 2. Using the np.zeros function: 3. Using the np.random.random_sample function: All these methods will create a 3-dimensional NumPy array of shape (3, 4, 5) and fill it with random values in the range [0, 1).
๐ŸŒ
learndataa
learndataa.com โ€บ codenotes โ€บ numpy-create-an-array-with-random-values
Numpy: Create an 1D, 2D, 3D arrays with random values โ€“ learndataa
July 31, 2020 - # Import library import numpy as np # Create 1D array: random floats x1 = np.random.randn(3) # Create 1D array: random integers x2 = np.random.randint(low=0, high=10, size=3) # Create 2D array: random floats x3 = np.random.randn(3,4) # Create 2D array: random integers x4 = np.random.randint(low=0, high=10, size=(3,4)) # Create 3D array: random floats x5 = np.random.randn(2,3,4) # Create 3D array: random integers x6 = np.random.randint(low=0, high=10, size=(2,3,4))
๐ŸŒ
Python Examples
pythonexamples.org โ€บ python-numpy-random-rand
Create Array with Random Values - NumPy Examples
To create a numpy array of specific shape with random values, use numpy.random.rand() with the shape of the array passed as argument.
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ python-random-array
Python random array
September 18, 2019 - The Python randn function generates random arrays of one, 2D, and 3D Arrays.
Find elsewhere
๐ŸŒ
GitHub
github.com โ€บ Cyril-Meyer โ€บ NumPyRandomShapes3D
GitHub - Cyril-Meyer/NumPyRandomShapes3D: Generate 3D NumPy array with random shapes
Random 3D Spheroid Random 3D Shapes ((64, 64, 64) array, 32 shapes with (32, 32, 32) max size) Random 3D Image ((128, 128, 128) array, 64 shapes with (64, 64, 64) max size)
Author: Cyril-Meyer
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ numpy-random-rand-python
numpy.random.rand() in Python - GeeksforGeeks
March 8, 2024 - The resulting array is then printed. ... # Python Program illustrating # numpy.random.rand() method import numpy as geek # 3D Array array = geek.random.rand(2, 2 ,2) print("\n\n3D Array filled with random values : \n", array);
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ random โ€บ generated โ€บ numpy.random.rand.html
numpy.random.rand โ€” NumPy v2.5 Manual
Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1). ... The dimensions of the returned array, must be non-negative. If no argument is given a single Python float is returned.
๐ŸŒ
CodeSpeedy
codespeedy.com โ€บ home โ€บ how to create matrix of random numbers in python โ€“ numpy
Create Matrix of Random Numbers in Python using NumPy - CodeSpeedy
April 16, 2019 - $ python codespeedy.py [0.13972036 0.58100399 0.62046278] The elements of the array will be greater than zero and less than one. 2d matrix using np.random.rand() import numpy as np random_matrix_array = np.random.rand(3, 4) print(random_matrix_array) Output: [[0.43189018 0.0903101 0.2664645 0.37512746] [0.63474244 0.91995859 0.84270619 0.97062349] [0.19307901 0.29623444 0.30945273 0.93585395]] Create a 3D matrix of random numbers in Python ยท
๐ŸŒ
Medium
medium.com โ€บ @whyamit404 โ€บ how-to-create-random-arrays-in-numpy-b22868e2d6aa
How to Create Random Arrays in NumPy? | by whyamit404 | Medium
March 6, 2025 - # Generate a 2D array with 3 rows and 4 columns random_array_2d = np.random.rand(3, 4) print(random_array_2d) ... # Generate a 3D array with shape (2, 3, 4) random_array_3d = np.random.rand(2, 3, 4) print(random_array_3d)
๐ŸŒ
SciPy
docs.scipy.org โ€บ doc โ€บ numpy-1.15.1 โ€บ reference โ€บ generated โ€บ numpy.random.rand.html
numpy.random.rand โ€” NumPy v1.15 Manual
August 23, 2018 - Random values in a given shape. Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1).
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ randomly selecting 1 element in a 3d array
r/learnpython on Reddit: Randomly selecting 1 element in a 3D Array
September 5, 2020 -

I have a 3D Numpy array (of user-specified dimensions) of 1s and 0s (how many 1s dependant on user input). For a part of the simulation, I have to pick a random element of this array.

I have tried using numpy.random.choice and random.sample but without any success. When I had to fill the 3d array with 1s, I had trouble selecting random elements to fill with 1s. The solution I found on google was reshaping to 1D, using random.choice and then reshaping back to 3D. It worked well.

My problem now is that the element I now choose at random, I have to also obtain the index for (so that I can reassign it from a 1 to a 0 or viceversa), so reshaping won't work since I would get indices of a 1d reshaped array. Furthermore the randomly selected element has to be a 0, that is a condition I am also struggling to implement with random.choice and random.sample.

My simulation has to run sometimes in a loop of 10000 operations. I have been trying to find an easy way to do this without making the simulation less efficient, so some approaches I am thinking of doing I haven't tried yet (I have thought of picking a 2d array within my array and then pick a row/column and then pick the element, but I am scared it will make my simulation take too long to run).

Can someone help me with this task? It's driving me nuts that something as "simple" as picking an element within a 3D array is so hard to implement.

๐ŸŒ
Tomasbeuzen
tomasbeuzen.com โ€บ python-programming-for-data-science โ€บ practice-exercises โ€บ chapter5-numpy-practice.html
NumPy โ€” Python Programming for Data Science
print(np.zeros(5)) print(np.ones(10)) print(np.full(5, 3.141)) print(np.array(range(21))) print(np.ones((5, 5), dtype=int)) [0. 0. 0. 0. 0.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [3.141 3.141 3.141 3.141 3.141] [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20] [[1 1 1 1 1] [1 1 1 1 1] [1 1 1 1 1] [1 1 1 1 1] [1 1 1 1 1]] ... Create an 3D matrix of 3 x 3 x 3 full of random numbers drawn from a standard normal distribution (hint: np.random.randn())
๐ŸŒ
SciPy
docs.scipy.org โ€บ doc โ€บ numpy-1.13.0 โ€บ reference โ€บ generated โ€บ numpy.random.rand.html
numpy.random.rand โ€” NumPy v1.13 Manual
Random values in a given shape. Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1).
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.0 โ€บ reference โ€บ random โ€บ generated โ€บ numpy.random.rand.html
numpy.random.rand โ€” NumPy v2.0 Manual
Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1). ... The dimensions of the returned array, must be non-negative. If no argument is given a single Python float is returned.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.1 โ€บ reference โ€บ random โ€บ generated โ€บ numpy.random.rand.html
numpy.random.rand โ€” NumPy v2.1 Manual
Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1). ... The dimensions of the returned array, must be non-negative. If no argument is given a single Python float is returned.