pythonnumpyrandom

Make a number more probable to result from random


I'm using x = numpy.random.rand(1) to generate a random number between 0 and 1. How do I make it so that x > .5 is 2 times more probable than x < .5?


Solution

  • That's a fitting name!

    Just do a little manipulation of the inputs. First set x to be in the range from 0 to 1.5.

    x = numpy.random.uniform(1.5)
    

    x has a 2/3 chance of being greater than 0.5 and 1/3 chance being smaller. Then if x is greater than 1.0, subtract .5 from it

    if x >= 1.0:
        x = x - 0.5