public
Created

  • Download Gist
gistfile1.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14
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
where
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'

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.