我需要从较长的列表中随机取样而不进行替换(每个元素仅在样本中出现一次).我正在使用下面的代码,但现在我想知道:
抽样的目的是能够概括从样本分析到人群的结果.
import System.Random
-- | Take a random sample without replacement of size size from a list.
takeRandomSample :: Int -> Int -> [a] -> [a]
takeRandomSample seed size xs
| size < hi = subset xs rs
| otherwise = error "Sample size must be smaller than population."
where
rs = randomSample seed size lo hi
lo = 0
hi = length xs - 1
getOneRandomV g lo hi = randomR (lo, hi) g
rsHelper size …Run Code Online (Sandbox Code Playgroud)