简体   繁体   中英

Generate random numbers in a specific range using beta distribution

I want to generate 1 Million and 10 Million data points in the range (0.0001,0.03) using a beta distribution with a=2.2 and b=1. Thanks in advance!

I tried this:

from scipy.stats import beta
import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots(1, 1)
a, b = 2.2, 1
#Generated pdf of required range
x = np.linspace(0.0001, 0.03, 100)

ax.plot(x, beta.pdf(x, a, b),
       'k-', lw=5, alpha=0.6, label='beta pdf')

r = beta.rvs(a, b, size=1000) #Generating values
print(r)

The values in 'r' are not in the range (0.0001,0.03).

For for alpha and beta parameters you are using, the beta distribution is a fairly straight line from (0, 0) to (1, 2.2) . The range you are interested in (0.0001, 0.03) is both a very thin slice of the 0 to 1 range, but also has a very small probability for the parameters you selected.

To actually generate 1M or 10M points, you will need to keep generating points and accumulating them to an array.

from scipy.stats import beta
import numpy as np

b_dist = beta(a=2.2, b=1)
target_length = 1_000_000
points = np.array([])

while points.size < target_length:
    # generate new points
    x = b_dist.rvs(1_000_000)
    # find the indices that match criteria
    ix = x < 0.03
    ix = ix & (0.0001 < x)
    points = np.hstack([points, x[ix]])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM