3

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.

Community
  • 1
  • 1
218
  • 1,754
  • 7
  • 27
  • 38

2 Answers2

0

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.

pjs
  • 18,696
  • 4
  • 27
  • 56
0

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.

boygood
  • 103
  • 1
  • 10
  • When max is sufficiently big you can use the inversion method https://stackoverflow.com/questions/2106503/pseudorandom-number-generator-exponential-distribution – boygood Oct 02 '18 at 06:13