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 7816157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:45:54+00:00 2026-06-02T05:45:54+00:00

Trying to solve problem 23 of 99 Haskell Problems. And I wrote this rnd_select

  • 0

Trying to solve problem 23 of 99 Haskell Problems.

And I wrote this

rnd_select :: (Eq a) => [a] -> Int -> [a]
rnd_select [] _ = []
rnd_select _ 0 = []
rnd_select ys n = 
   let 
       (rnd_index, gen) = randomR (1, length ys) (mkStdGen 200)
       (x, xs) = removeAt rnd_index ys
   in x : rnd_select xs (n-1)

which works but I dont want use mkStdGen but use

  newStdGen or getStdGen

instead.
I have seen the solutions to the problem but I want to understand how should I fix this code to do that and if its not possible why not because intuitively it feels like it should work but it doesn’t.

  • 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-02T05:45:55+00:00Added an answer on June 2, 2026 at 5:45 am

    Remember that Haskell functions are pure; they must always return the same result given the same input. You could make your function return IO [a] instead, which would let you call newStdGen, but a better way is to keep your code pure by taking the random number generator as an additional argument to your function and also returning the new generator afterwards:

    rnd_select :: (Eq a, RandomGen g) => [a] -> Int -> g -> ([a], g)
    rnd_select [] _ gen = ([], gen)
    rnd_select _ 0  gen = ([], gen)
    rnd_select ys n gen = 
       let (rnd_index, gen') = randomR (1, length ys) gen
           (x, xs) = removeAt rnd_index ys
           (xs', gen'') = rnd_select xs (n-1) gen'
       in (x : xs', gen'')
    

    Now you can use it with, e.g. getStdRandom :: (StdGen -> (a, StdGen)) -> IO a like this.

    > getStdRandom (rnd_select [1..20] 10)
    [12,11,14,4,16,7,1,2,18,15]
    

    Passing the generators around manually can be somewhat tedious, though. One way of making this neater is to use the MonadRandom package.

    rnd_select :: (MonadRandom m, Eq a) => [a] -> Int -> m [a]
    rnd_select [] _ = return []
    rnd_select _ 0  = return []
    rnd_select ys n = do
      rnd_index <- getRandomR (1, length ys)
      let (x, xs) = removeAt rnd_index ys
      xs' <- rnd_select xs (n-1)
      return (x:xs')
    

    Since IO is an instance of MonadRandom, you can use this directly as an IO action.

    > rnd_select [1..20] 10
    [20,18,12,13,5,7,17,9,3,4]
    > rnd_select [1..20] 10
    [9,18,4,20,6,5,3,15,13,7]
    

    or you can use evalRand to run this in a pure monad, providing your own random number generator so you can get repeatable results (good for debugging / testing).

    > evalRand (rnd_select [1..20] 10) (mkStdGen 200)
    [4,16,15,13,8,20,6,14,5,3]
    > evalRand (rnd_select [1..20] 10) (mkStdGen 200)
    [4,16,15,13,8,20,6,14,5,3]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to solve this problem in Haskell but getting time limit exceed.
I am trying to solve problem 10 from the ninety nine haskell problems. Here
I am very interested in Haskell and currently trying to solve this problem. To
Trying to solve this problem . I would like to learn how the bootstrapper
I am trying to solve this problem. I have a series of SELECT statements
I'm trying to solve this problem : http://uva.onlinejudge.org/external/7/732.html . For the given example, they
I'm trying to solve this problem, its not a homework question, its just code
I'm trying to solve a problem which is something like this: I'm given n
I was trying to solve this problem in UVa, but I get a NullPointerException
When trying to solve this problem , I encountered some articles, etc. referring to

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.