+4 votes
in Programming Languages by (40.5k points)
edited by
I want to select "k" uniformly distributed numbers between two numbers n1 and n2. What Python function should I use?

1 Answer

+3 votes
by (348k points)
selected by
 
Best answer

You can use Numpy's uniform() function to draw samples from the parameterized uniform distribution. Using parameters low, high, and size, you can specify the samples' lower boundary, upper boundary, and size, respectively.

Here is an example to select 10 uniformly distributed samples between 4 and 10.

>>> import numpy as np
>>> rnd=np.random.default_rng()
>>> rnd.uniform(low=4, high=10, size=10)
array([7.82391248, 4.56017423, 9.31976651, 4.04291941, 8.28304775,
       5.7652216 , 7.87284321, 4.79759185, 4.04867037, 8.36414237])


...