Logo

Programming-Idioms

History of Idiom 11 > diff from v65 to v66

Edit summary for version 66 by hrschueler:
[Haskell] RandomRIO produces an IO object, which is not always desirable. This code takes a generator from the IO part of the code

Version 65

2018-01-14, 17:28:53

Version 66

2018-04-17, 16:49:18

Idiom #11 Pick a random element from a list

List x must be non-empty.

Illustration

Idiom #11 Pick a random element from a list

List x must be non-empty.

Illustration
Extra Keywords
choose
Extra Keywords
choose
Imports
import System.Random (randomRIO)
Imports
import System.Random (randomR)
Code
(l !!) <$> (randomRIO (0, length l))
Code
(\l g -> l !! (randomR (0, length l) g))