Search code examples
pythonpython-3.xrandomnormal-distribution

normalvariate with bounds?


Is it possible to use python's functions in random module like normalvariate or gammavariate, but with some lower and/or upper bounds?. In short, I'm looking if there is a more efficient way to do this:

temp = normalvariate(mu, sigma)
while not (lower_bound <= temp <= upper_bound):
    temp = normalvariate(mu, sigma)
do_something(temp)

Solution

  • I decided that for the context applied, it's not worth to require such a heavy module as scipy or numpy, so I just did what I posted on my question, until I find something efficient and light weight.