How to generate n dimensional random variables in a specific range in python

I want to generate uniform random variables in a range of [-10,10]different dimensions in python. Numbers 2,3,4,5 .... dimension.

I tried random.uniform (-10,10), but this is only one dimension. I do not know how to do this for n-dimensionality. By 2 dimensions, I mean

[[1 2], [3 4]...]
+5
source share
2 answers

Since numpytagged, you can use random functions in numpy.random:

>>> import numpy as np
>>> np.random.uniform(-10,10)
7.435802529756465
>>> np.random.uniform(-10,10,size=(2,3))
array([[-0.40137954, -1.01510912, -0.41982265],
       [-8.12662965,  6.25365713, -8.093228  ]])
>>> np.random.uniform(-10,10,size=(1,5,1))
array([[[-3.31802611],
        [ 4.60814984],
        [ 1.82297046],
        [-0.47581074],
        [-8.1432223 ]]])

and change the setting sizeto suit your needs.

+10
source

use random.uniform

import random

random_variable_in_range_of_minus_ten_and_plus_ten = random.uniform(-10, 10)

note that it is between (by design) [-10, 10] not [-10, 10]

n-, , , , n , :

def n_dimensional_random_variables(n, lbound=-10, rbound=10):
  return [random.uniform(lbound, rbound) for i in xrange(n)]
+2

All Articles