Java: random integer with non-uniform distribution

前端 未结 11 1650
终归单人心
终归单人心 2020-12-13 12:55

How can I create a random integer n in Java, between 1 and k with a "linear descending distribution", i.e. 1 is

11条回答
  •  渐次进展
    2020-12-13 13:16

    There are lots of ways to do this, but probably the easiest is just to generate two random integers, one between 0 and k, call it x, one between 0 and h, call it y. If y > mx + b (m and b chosen appropriately...) then k-x, else x.

    Edit: responding to comments up here so I can have a little more space.

    Basically my solution exploits symmetry in your original distribution, where p(x) is a linear function of x. I responded before your edit about generalization, and this solution doesn't work in the general case (because there is no such symmetry in the general case).

    I imagined the problem like this:

    1. You have two right triangles, each k x h, with a common hypotenuse. The composite shape is a k x h rectangle.
    2. Generate a random point that falls on each point within the rectangle with equal probability.
    3. Half the time it will fall in one triangle, half the time in the other.
    4. Suppose the point falls in the lower triangle.
      • The triangle basically describes the P.M.F., and the "height" of the triangle over each x-value describes the probability that the point will have such an x-value. (Remember that we're only dealing with points in the lower triangle.) So by yield the x-value.
    5. Suppose the point falls in the upper triangle.
      • Invert the coordinates and handle it as above with the lower triangle.

    You'll have to take care of the edge cases also (I didn't bother). E.g. I see now that your distribution starts at 1, not 0, so there's an off-by-one in there, but it's easily fixed.

提交回复
热议问题