简体   繁体   中英

I would like to know how how to generate random numbers with a specific normalized distribution funtion

Basically, if my distribution function is f(v)= NormalDistribution(-u,sigma)+ NormalDistribution(u,sigma)

How do I define f as a PDF, normalize it and then apply some random variate command to my PDF?

Well, you have sum of two normalized Gaussian,

f(x) = N(x|μ,σ) + N(x|-μ,σ)

∫ f(x) dx = 1 + 1 = 2

PDF(x|μ,σ) = N(x|μ,σ)/2 + N(x|-μ,σ)/2

Because PDF is symmetric, sampling is simple: select one gaussian or another with 50% probability

Along the lines (untested)

import random

def sample(μ, σ):
    if random.random() < 0.5:
        return random.gauss(μ,σ)
    return random.gauss(-μ,σ)

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