Let's say I have to generate random number from 1 to 100, however, the probability of each number is not 1/100, but a predefined probability.
How to generate that number? I use Ruby/Python.
Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle. It only takes a minute to sign up.
Sign up to join this communityLet's say I have to generate random number from 1 to 100, however, the probability of each number is not 1/100, but a predefined probability.
How to generate that number? I use Ruby/Python.
the algorithm itself is easy: create an array with all the weights' cumulative sum. After that you generate a [0-1)
uniform random number and just binary search that number multiplied with the sum of the weights and the index will be the number you will need to generate.