Random numbers with orders less than E-1

175 views Asked by At

I'm currently working on a kinetic Monte Carlo simulation in Python, so I need to generate pseudo-random numbers between 0 and 1 that follow the uniform distribution, but I'm searching to obtain at some point numbers less than E-1, for example, 0.001. I know that it won't be common, because they are random numbers after all, but my problem started in the implementation of Monte Carlo.

The problem in my Monte Carlo simulation, briefly is, I have one constraint_probability=c and one pseudo-random number=r, but because of the dynamics of my problem, I have the condition that my code only run if r < c. If I make for example c=0.001, my code never run.

My first thought was to use random.uniform, but I found that the numbers obtained were at least with an order of E-2.

Later, I thought that my problem could be solved by making the following function:

from scipy.stats import uniform


def numrand():
        vec=np.linspace(uniform.ppf(0),uniform.ppf(1), 10000) 
        return random.choice(vec)

I don't know if it's possible to obtain random numbers less than E-1 or if is another way to make my code run.

2

There are 2 answers

0
pho On

You can tell np.random.uniform the max and min value that you want. Since you seem to want numbers of the order ~10-5, ask for min=1e-5 and max=1e-4:

vec = np.random.uniform(1e-5, 1e-4, 10000)
assert (vec >= 1e-5).all() and (vec < 1e-4).all()

Since samples are uniformly distributed over the half-open interval [min, max), you can rest assured that any number you get from the distribution will be of the order 10-5

This should also work with other distributions.

0
asdf101 On

numpy.random can likely help you here, to create 10000 random numbers between 0 and 1 with uniform distribution you can use

np.random.default_rng().uniform(0, 1, 10000)

or for older numpy versions:

np.random.uniform(0, 1, 10000)