dimanche 11 décembre 2016

Random selection between 2 or more ranges

I'm trying to make a random number generator which will pick more "evenly" between 3 and 4 digit ranges. If I just do:

result = rand(100..9999)

I'm well aware that in the majority of cases, a 4 digit number will be selected. I want to give 3-digit numbers more chance of being selected, so I did this:

rand_3_digit_num = (100..999)
rand_4_digit_num = (1000..9999)

rand([rand_3_digit_num, rand_4_digit_num].sample)

Is there any other approach to do this? My goal is to just give 3-digit numbers bigger chance of being selected than with an ordinary rand. This problem gets even worse if I introduce 5-digit or 6-digit numbers, the chance of 3-digit or 4-digit numbers to get selected quickly diminishes.




Aucun commentaire:

Enregistrer un commentaire