问题
I would like to generate random integers on an interval min to max. For a uniform distribution in numpy:
numpy.random.randint(min,max,n)
does exactly what I want.
However, I would now like to give the distribution of random numbers an exponential bias. There are a number of suggestions for this e.g. Pseudorandom Number Generator - Exponential Distribution as well as the numpy function numpy.random.RandomState.exponential
, but these do not address how to constrain the distribution to integers between min and max. I'm not sure how to do this, whilst still ensuring a random distribution.
回答1:
The exponential distribution is a continuous distribution. What you probably want is its discrete equivalent, the geometric distribution. Numpy's implementation generates strictly positive integers, i.e, 1,2,3,..., so you'll want add min-1
to shift it, and then truncate by rejecting/throwing away results > max
. That, in turn, means generating them one-by-one add adding the non-rejected values to a list until you get the desired number. (You could also determine analytically what proportion you expect to be rejected, and scale your n
accordingly, but you'll still likely end up a few short or with a few too many.)
It's possible to do this without rejection, but you'd have to create your own inversion, determine the probability of exceeding max
, and generate uniforms to between 0 and that probability to feed to your inversion algorithm. Rejection is simpler even though it's less efficient.
回答2:
May be you can try summing up all the bias. Then the probability of generating an integer j= bias of j / total bias
. You can use monte carlo simulation to implement this.
来源:https://stackoverflow.com/questions/24395258/random-integers-from-an-exponential-distribution-between-min-and-max