First guess ... choose a random number between 1 and 2**size, find the base 2 of the journal and select the number that remains from the set of elements.
Forgive my terrible ruby skill.
return a[-((Math.log(rand(2**size-1)+1) / Math.log(2)).floor) - 1]
rand 0, . 1 2, . 3, 4, 5 6, . .. , .
Edit: , , log2, log/log (2).
return a[-(Math.log2(rand(2**size - 1)+1).floor) - 1]
, ,
return a[-((rand(2**size-1)+1).to_s(2).length)]
String. , , .:)
: , , +1 -1. , . ( , .)
Edit: ** -, ( Ruby , ).
return a[-(rand(1<<size).to_s(2).length)]