Random sampling from a discrete interval is relatively simple. Here my code is Python, but also straightforward in R and others.

position = random.randint(1, 100)

I would like to introduce a small bias in the sampling so that it's slightly more likely to draw from the range [75, 100]. My naive first attempt was as follows.

position = random.randint(1, 100 + 25)
if position > 100:
    position -= 25

However this results in very elevated sampling at both ends of the interval. How would I 1) restrict the bias toward only one end of the interval; and 2) control the intensity of the bias?


As the question was asked in terms of Python, I will answer in the same way:

You can use numpy.random.choice(), which allows you to specify arbitrary weighting, via the p parameter.

The only annoying thing is that it requires p to be a probability array with sum = 1, rather than allowing an arbitrary weight array with entries >= 0. (So I always end up making a normalize_sum command.)

| cite | improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.