Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a list which contains a given number of random numbers within a range in Haskell?

I know how to generate a single random number within a given range, a list of random numbers, a list which contains a given number of random numbers but NOT a list which contains a given number of random numbers within a RANGE. Can anyone help me on this?

This code (extracted from haskell.org) generates a list of 10 random numbers, but I need to give a range, any ideas on how to edit this to give a range?

import System.Random
import Data.List

main = do
 seed  <- newStdGen
 let rs = randomlist 10 seed
 print rs

randomlist :: Int -> StdGen -> [Int]
randomlist n = take n . unfoldr (Just . random)
like image 756
n00b Avatar asked Dec 05 '22 18:12

n00b


1 Answers

randomList :: (Random a) => (a,a) -> Int -> StdGen -> [a]
randomList bnds n = take n . randomRs bnds

Using randomRs from System.Random.

like image 200
ivanm Avatar answered Jan 08 '23 14:01

ivanm