How to randomly generate really small numbers? How to randomly generate really small numbers? python-3.x python-3.x

How to randomly generate really small numbers?


You need to think closely about what you're doing. You're asking for a uniform distribution between almost 0.0 and 0.1. The average result would be 0.05. Which is exactly what you're getting. It seems you want a random distribution of the exponents.

The following might do what you want:

import randomdef rnd():    exp = random.randint(-19, -1)    significand = 0.9 * random.random() + 0.1    return significand * 10**exp[rnd() for _ in range(20)]

The lowest possible value is when exp=-19 and significand=0.1 giving 0.1*10**-19 = 1**-20. And the highest possible value is when exp=-1 and significand=1.0 giving 1.0*10**-1 = 0.1.

Note: Technically, the significand can only aprach 1.0 as random() is bounded to [0.0, 1.0), i.e., including 0.0, but excluding 1.0.

Output:

[2.3038280595190108e-11, 0.02658855644891981, 4.104572641101877e-11, 3.638231824527544e-19, 6.220040206106022e-17, 7.207472203268789e-06, 6.244626749598619e-17, 2.299282102612733e-18, 0.0013251357609258432, 3.118805901868378e-06, 6.585606992344938e-05, 0.005955900790586139, 1.72779538837876e-08, 7.556972406280229e-13, 3.887023124444594e-15, 0.0019965330694999488, 1.7732147730252207e-08, 8.920398286274208e-17, 4.4422869312622194e-08, 2.4815949527034027e-18]

See "scientific notation" on wikipedia for definition of significand and exponent.


As per the numpy documentation:

low : float or array_like of floats, optionalLower boundary of the output interval. All values generated will be greater than or equal to low. The default value is 0.

With that in mind, decreasing the value of low will produce lower numbers

>>> np.random.uniform(0.00001, 10**(-20))6.390804027773046e-06


How about generating a random number between 1 and 10 000, then divide that number by 100 000.