My Coding > Programming language > Python > Python libraries and packages > Python NumPy > How to make random Numpy 2D array with different range in every column

How to make random Numpy 2D array with different range in every column

Sometimes it is necessary to generate set of objects with random properties. Every object have few parameters which can be defined in different ranges and it is necessary to generate all of them in one go. This task is refereed to the question: How to make random Numpy 2D array with different range in every column.

Example of task for making random Numpy 2D array with different range in every column

Let’s consider simple task. We have 3 dimensional box with sizes A, B and C, and in this box we need randomly place N particles which mass can change form X to Y.

Therefore, we need to make array with N rows and 4 columns in each row

Generating random array

First of all, it is necessary to define lists with lowest and highest possible parameters:.


param_min = [0, 0, 0, X]
param_max = [A, B, C, Y]

And then, it is necessary to call function numpy.random.uniform with giving all these parameters:


res = np.random.uniform(low=param_min, high=param_max, size=(N, 4))

Final code

The final code with some values will be:


import numpy as np
A, B, C = 3, 4, 5
X, Y = 10, 20
N = 6
param_min = [0, 0, 0, X]
param_max = [A, B, C, Y]
res = np.random.uniform(low=param_min, high=param_max, size=(N, 4))
print(res)

and the output will be something like:


[[ 2.75674107  1.87257567  1.25998458 16.19081678]
 [ 2.57195988  0.15014899  1.13814381 15.87721651]
 [ 0.86021531  1.74379214  4.37837674 13.17432844]
 [ 1.43378229  3.62513605  2.998748   17.98741841]
 [ 0.39007479  2.19313939  3.49227668 17.31299908]
 [ 0.84030949  1.55642955  1.36390591 10.18458593]]


Published: 2022-09-24 22:31:42

Last 10 artitles


9 popular artitles

© 2020 MyCoding.uk -My blog about coding and further learning. This blog was writen with pure Perl and front-end output was performed with TemplateToolkit.