I need to generate a random number from the following data:
0-10 : 23%
10-80 : 50%
80-100 : 27%
How do I generate a random number from such information?
One way would be to fit a distribution but I have to do this for about hundred variables and I don’t want to fit 100 distributions. Any hints?
import random
a=[23, 73, 100]
b=[10, 80, 100]
rndval=awesomefunction(a,b)
Now, regarding awesomefunction(), I have absolutely no clue.
But, from what little I know, (and a very sloppy implementation)
temp_rand=random.uniform(0,100)
if(temp_rand<=23):
rndval=random.uniform(0,10)
if(temp_rand<=73 && temp_rand>23):
rndval=random.uniform(10,80)
if(temp_rand>73):
rndval=random.uniform(80,100)
But IMHO, this is sloppy beyond measure.
You can have 2 random generators to achieve this. The first should generate a random double from 0 to 1.
For this generator, you should check to see if the random number is from 0 to .22, .23 to .73, or .74 to 1. If the number falls within the first range, you just run another random number generator that generates a number from 0 to 10, and that’s your number. The same goes for the second and third ranges.
Some high level pseudocode:
A side note: Most languages already have random number generators that will generate a random number from 0 to 1 or an integer up to a specific range (0 to 10).