Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8969937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:39:50+00:00 2026-06-15T17:39:50+00:00

I need to take a random sample without replacement (each element only occurring once

  • 0

I need to take a random sample without replacement (each element only occurring once in the sample) from a longer list. I’m using the code below, but now I’d like to know:

  1. Is there a library function that does this?
  2. How can I improve this code? (I’m a Haskell beginner, so this would be useful even if there is a library function).

The purpose of the sampling is to be able to generalize findings from analyzing the sample to the population.

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 lo hi g x acc
    | x `notElem` acc && length acc < size = rsHelper size lo hi new_g new_x (x:acc)
    | x `elem` acc && length acc < size = rsHelper size lo hi new_g new_x acc
    | otherwise = acc
    where (new_x, new_g) = getOneRandomV g lo hi

-- | Get a random sample without replacement of size size between lo and hi.
randomSample seed size lo hi = rsHelper size lo hi g x [] where
(x, g)  = getOneRandomV (mkStdGen seed) lo hi

subset l = map (l !!) 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:39:51+00:00Added an answer on June 15, 2026 at 5:39 pm

    Here’s a quick ‘back-of-the-envelope’ implementation of what Daniel Fischer suggested in his comment, using my preferred PRNG (mwc-random):

    {-# LANGUAGE BangPatterns #-}
    
    module Sample (sample) where
    
    import Control.Monad.Primitive
    import Data.Foldable (toList)
    import qualified Data.Sequence as Seq
    import System.Random.MWC
    
    sample :: PrimMonad m => [a] -> Int -> Gen (PrimState m) -> m [a]
    sample ys size = go 0 (l - 1) (Seq.fromList ys) where
        l = length ys
        go !n !i xs g | n >= size = return $! (toList . Seq.drop (l - size)) xs
                      | otherwise = do
                          j <- uniformR (0, i) g
                          let toI  = xs `Seq.index` j
                              toJ  = xs `Seq.index` i
                              next = (Seq.update i toI . Seq.update j toJ) xs
                          go (n + 1) (i - 1) next g
    {-# INLINE sample #-}
    

    This is pretty much a (terse) functional rewrite of R’s internal C version of sample() as it’s called without replacement.

    sample is just a wrapper over a recursive worker function that incrementally shuffles the population until the desired sample size is reached, returning only that many shuffled elements. Writing the function like this ensures that GHC can inline it.

    It’s easy to use:

    *Main> create >>= sample [1..100] 10
    [51,94,58,3,91,70,19,65,24,53]
    

    A production version might want to use something like a mutable vector instead of Data.Sequence in order to cut down on time spent doing GC.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to take random row from table. At first, i wrote SP like
Need to take a SELECT drop down list options and find if any of
i need to take some row. They came from sql TARIH (sql column) is
I need to take rows from two separate tables, and arrange them in descending
I need to generate random numbers from Binomial(n, p) distribution. A Binomial(n, p) random
I have a 'list' of objects, from which I want to take object at
I'm looking for a solution to following task. I take few random pages from
I need to calculate the approximate amount of time an algorithm will take without
I have file consists of 10000 different lines. I need to take 100 random
I need a photo album extension for Joomla that can take selected folders from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.