Haskell, 7878 77 bytes
import System.Random
f l=randomRIO(0,sum l-1)>>=print>>=pure.((l>>= \n->n<$[1..n])!!)
Try it online!Try it online! Usage example: f [1,99]
probably yields 99
.
###Explanation:
f
takes a list of integersl
and printsreturns the randomly selected integer asIO Int
.l>>= \n->n<$[1..n]
constructs a list with each elementn
repeatedn
times.randomRIO(0,sum l-1)
yields an integer in the range from 0 to the length of the list of repeated elements, which is exactly the sum of all elements, minus one to avoid a out of bound exception.
Bonus: 85 byte point-free version
import System.Random
(>>=).randomRIO.(,)0.pred.sum<*>(pure.).(!!).(>>= \n->n<$[1..n])
[Try it online!][TIO-j7kv8s6o] [TIO-j7kv8s6o]: https://tio.run/##HYqxCsMgFAD3fsULdNCSPnSP7p0K6ZhmEGKoNL7I0wz9emsLx8HBvVx@@22rNcS0c4HHJxcfcXS07PG0GmGtkcj/HG93FL1UmNgvmI84XKxIB3uUKLquqc3wpKul4TxpRJpljS4QGFhh0r1WDTXDb0scqNQv "Haskell – Try It Online"