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
?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
if you want a more fluid randomness, you can just square the output of the random function
(and subtract it from 1 to make
x > 0.5
more probable instead ofx < 0.5
).This is overkill for you, but it's good to know an actual method for generating a random number with any probability density function (pdf).
You can do that by subclassing scipy.stat.rv_continuous, provided you do it correctly. You will have to have a normalized pdf (so that its integral is 1). If you don't, numpy will automatically adjust the range for you. In this case, your pdf has a value of 2/3 for x<0.5, and 4/3 for x>0.5, with a support of [0, 1) (support is the interval over which it's nonzero):
You could take a "mixture model" approach where you split the process into two steps: first, decide whether to take option A or B, where B is twice as likely as A; then, if you chose A, return a random number between 0.0 and 0.5, else if you chose B, return one between 0.5 and 1.0.
In the example, the randint randomly returns 0, 1, or 2, so the
else
case is twice as likely as theif
case.This is a little more expensive (two random draws instead of one) but it can generalize to more complicated distributions in a fairly straightforward way.
First off,
numpy.random.rand(1)
doesn't return a value in the[0,1)
range (half-open, includes zero but not one), it returns an array of size one, containing values in that range, with the upper end of the range having nothing to do with the argument passed in.The function you're probably after is the uniform distribution one,
numpy.random.uniform()
since this will allow an arbitrary upper range.And, to make the upper half twice as likely is a relatively simple matter.
Take, for example, a random number generator
r(n)
which returns a uniformly distributed integer in the range[0,n)
. All you need to do is adjust the values to change the distribution:Now the chances of getting zero are 1/3 while the chances of getting one are 2/3, basically what you're trying to achieve with your floating point values.
So I would simply get a random number in the range
[0,1.5)
, then subtract 0.5 if it's greater than or equal to one.Since the original distribution should be even across the
[0,1.5)
range, the subtraction should make[0.5,1.0)
twice as likely (and[1.0,1.5)
impossible), while keeping the distribution even within each section ([0,0.5)
and[0.5,1)
):is pretty easy way to do it
ehh I guess this is 3x as likely ... thats what i get for sleeping through that class I guess
That's a fitting name!
Just do a little manipulation of the inputs. First set
x
to be in the range from0
to1.5
.x
has a2/3
chance of being greater than0.5
and1/3
chance being smaller. Then ifx
is greater than1.0
, subtract.5
from it