Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate random int in 3D array

l would like to generate a random 3d array containing random integers (coordinates) in the intervalle [0,100].

so, coordinates=dim(30,10,2)

What l have tried ?

coordinates = [[random.randint(0,100), random.randint(0,100)] for _i in range(30)]

which returns

array([[97, 68],
       [11, 23],
       [47, 99],
       [52, 58],
       [95, 60],
       [89, 29],
       [71, 47],
       [80, 52],
       [ 7, 83],
       [30, 87],
       [53, 96],
       [70, 33],
       [36, 12],
       [15, 52],
       [30, 76],
       [61, 52],
       [87, 99],
       [19, 74],
       [37, 63],
       [40,  2],
       [ 8, 84],
       [70, 32],
       [63,  8],
       [98, 89],
       [27, 12],
       [75, 59],
       [76, 17],
       [27, 12],
       [48, 61],
       [39, 98]])

of shape (30,10)

What l'm supposed to get ?

dim=(30,10,2) rather than (30,10)

like image 330
eric lardon Avatar asked Mar 28 '18 15:03

eric lardon


People also ask

How do you create a random 3d numpy array?

To create a numpy array of specific shape with random values, use numpy. random. rand() with the shape of the array passed as argument.

How do you generate a random number in an array?

In order to generate random array of integers in Java, we use the nextInt() method of the java. util. Random class. This returns the next random integer value from this random number generator sequence.

What is random rand () function in numpy?

The numpy.random.rand() function creates an array of specified shape and fills it with random values. Syntax : numpy.random.rand(d0, d1, ..., dn) Parameters : d0, d1, ..., dn : [int, optional]Dimension of the returned array we require, If no argument is given a single Python float is returned.


1 Answers

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

like image 139
unutbu Avatar answered Oct 08 '22 19:10

unutbu