Hi All, Can anyone help me I want to produce a list of three random numbers for e.g. [7,8,1] I tried using x <- getStdRandom $ randomR (1,10) but don't really understand this and it only generates one number. Any help greatly appreciated. Regards John
On Sat, Oct 24, 2009 at 05:59:35PM +0100, John Moore wrote:
Hi All, Can anyone help me I want to produce a list of three random numbers for e.g. [7,8,1] I tried using x <- getStdRandom $ randomR (1,10) but don't really understand this and it only generates one number. Any help greatly appreciated.
replicateM is your friend: replicateM :: (Monad m) => Int -> m a -> m [a] so if 'foo' produces a single random number, then 'replicateM 3 foo' produces a list of three. -Brent
Or just randomRs :: (Random a, RandomGen g) => (a,a) -> g -> [a] Bob On Sun, Oct 25, 2009 at 2:05 AM, Brent Yorgey <byorgey@seas.upenn.edu>wrote:
On Sat, Oct 24, 2009 at 05:59:35PM +0100, John Moore wrote:
Hi All, Can anyone help me I want to produce a list of three random numbers for e.g. [7,8,1] I tried using x <- getStdRandom $ randomR (1,10) but don't really understand this and it only generates one number. Any help greatly appreciated.
replicateM is your friend:
replicateM :: (Monad m) => Int -> m a -> m [a]
so if 'foo' produces a single random number, then 'replicateM 3 foo' produces a list of three.
-Brent _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
Hi John, When I was first encountered replicateM I found it really hard to understand. So,of course, I am audacious enough to assume that it is hard for you too! The code suggested by Brent , 'replicateM 3 foo' is a nicer way of writing the following: foo = do x <- getStdRandom $ randomR (1,10) y <- getStdRandom $ randomR (1,10) z <- getStdRandom $ randomR (1,10) return [x,y,z] Hope this helped. -deech On Sat, Oct 24, 2009 at 7:05 PM, Brent Yorgey <byorgey@seas.upenn.edu>wrote:
On Sat, Oct 24, 2009 at 05:59:35PM +0100, John Moore wrote:
Hi All, Can anyone help me I want to produce a list of three random numbers for e.g. [7,8,1] I tried using x <- getStdRandom $ randomR (1,10) but don't really understand this and it only generates one number. Any help greatly appreciated.
replicateM is your friend:
replicateM :: (Monad m) => Int -> m a -> m [a]
so if 'foo' produces a single random number, then 'replicateM 3 foo' produces a list of three.
-Brent _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (4)
-
aditya siram -
Brent Yorgey -
John Moore -
Tom Davie