This question already has an answer here:
- Random weighted choice 7 answers
Let say I have sorted array :
ary = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
the goal is to randomly pick a number with HIGHER probability of selecting lower numbers. One way to do that is the following :
ary[ int( rand() * rand() * 10) ]
or even :
ary[ int( rand() * rand() * 11) ]
multiplying random numbers 0 < x < 1, on average will produce smaller numbers.
I was wondering if you know better more robust way to that ? Perfectly the "probability slope" of the chance of selecting number should approximate something like 1/x function or something like that.
Aucun commentaire:
Enregistrer un commentaire