1

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.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Henieheb
  • 13
  • 2
  • 1
    Does this answer your question? [A weighted version of random.choice](https://stackoverflow.com/questions/3679694/a-weighted-version-of-random-choice) – Peter O. Nov 17 '20 at 10:58

1 Answers1

1

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)

Niels Uitterdijk
  • 713
  • 9
  • 27