Henieheb
Henieheb

Reputation: 13

Generate random numbers with a numerical distribution in given intervals

I have a file that can generate random numbers in a given interval

random.randint(0,30)

I want to assign certain intervals a distribution, such that for example

0-9 has a 50% chance of occurring, 10-19 has a 30% chance of occurring and 20-29 has a 20% chance of occurring.

Upvotes: 1

Views: 62

Answers (1)

Niels Uitterdijk
Niels Uitterdijk

Reputation: 770

The following will do the job. It first generates a uniform distributed random number to determine which interval, then a uniformly distributed integer in that interval.

import numpy as np


interval = np.random.rand()
if interval < 0.5:
    final = np.random.randint(0, 10)
elif interval < 0.8:
    final = np.random.randint(10, 20)
else:
    final = np.random.randint(20, 30)

Upvotes: 1

Related Questions