Create a gist now

Instantly share code, notes, and snippets.

import qualified Data.Vector as V
import qualified System.Random as R
sample :: R.RandomGen g => g -> Int -> [a] -> V.Vector a
sample r n l = go r vec0 (succ n) remainder where
vec0 = V.fromList beginning
(beginning, remainder) = splitAt n l
go _ v _ [] = v
go r v i (x:xs) = v `seq` go r'' updated (succ i) xs
updated | p < n = v V.// upd
| otherwise = v
upd = [(idx, x)]
(p, r') = rand0 where rand0 = R.randomR (0, i-1) r
(idx, r'') = rand1 where rand1 = R.randomR (0, n-1) r'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment