Created
April 28, 2019 21:46
-
-
Save dionyziz/381784d80583e715156e62b2e015676e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Control.Monad.State.Lazy | |
import System.Random | |
import RemoveAt | |
choose = do | |
(gen, xs) <- get | |
let (idx, gen') = randomR (1, length xs) gen | |
let (val, xs') = removeAt idx xs | |
put (gen', xs') | |
return val | |
chooseN gen 0 _ = (gen, []) | |
chooseN gen n xs = | |
let (result, (gen', xs')) = runState choose (gen, xs) | |
(gen'', rest) = chooseN gen' (n - 1) xs' | |
in (gen'', result:rest) | |
rnd_select :: [a] -> Int -> IO [a] | |
rnd_select xs n = do | |
gen <- getStdGen | |
let (gen', choice) = (chooseN gen n xs) | |
setStdGen gen' | |
return choice |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment