Skip to main content
added 396 characters in body
Source Link
Laikoni
  • 26.3k
  • 6
  • 52
  • 115

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 integers l and printsreturns the randomly selected integer as IO Int.
  • l>>= \n->n<$[1..n] constructs a list with each element n repeated n 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"

Haskell, 78 bytes

import System.Random
f l=randomRIO(0,sum l-1)>>=print.((l>>= \n->n<$[1..n])!!)

Try it online! Usage example: f [1,99] probably yields 99.

###Explanation:

  • f takes a list of integers l and prints the randomly selected integer.
  • l>>= \n->n<$[1..n] constructs a list with each element n repeated n 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.

Haskell, 78 77 bytes

import System.Random
f l=randomRIO(0,sum l-1)>>=pure.((l>>= \n->n<$[1..n])!!)

Try it online! Usage example: f [1,99] probably yields 99.

###Explanation:

  • f takes a list of integers l and returns the randomly selected integer as IO Int.
  • l>>= \n->n<$[1..n] constructs a list with each element n repeated n 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"

Source Link
Laikoni
  • 26.3k
  • 6
  • 52
  • 115

Haskell, 78 bytes

import System.Random
f l=randomRIO(0,sum l-1)>>=print.((l>>= \n->n<$[1..n])!!)

Try it online! Usage example: f [1,99] probably yields 99.

###Explanation:

  • f takes a list of integers l and prints the randomly selected integer.
  • l>>= \n->n<$[1..n] constructs a list with each element n repeated n 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.