
I need some help to do a function so that I cant get 4 numbers between 1 and 6, i tried to use random but i can't make it work well.

On Tue, 17 Dec 2002, Filipe Santos wrote:
I need some help to do a function so that I cant get 4 numbers between 1 and 6, i tried to use random but i can't make it work well.
This might be useful, import Random dice :: (RandomGen g) => g -> Int -> (g, [Int]) dice rng number_rolls = (iterate next_roll (rng, [])) !! number_rolls where next_roll (rng, rolls) = let (random_number, new_rng) = next rng in (new_rng, (mod random_number 6 + 1) : rolls) main = do rng <- newStdGen -- (or getStdGen or something) print (snd (dice rng 4)) The "dice" function gives you back the new state of the rng which maybe you should keep around to start the next set of rolls with. ("main" throws it away with "snd".) I'd certainly be interested to see how this could be written more nicely. I avoided making the list of die rolls an infinitely long lazy list in case we wanted to use the same RNG for other stuff too and not have the same RNs reused, but I don't know much about how the RNG stuff is exactly defined. -- Mark

Mark Carroll writes:
The "dice" function gives you back the new state of the rng which maybe you should keep around to start the next set of rolls with. ("main" throws it away with "snd".) I'd certainly be interested to see how this could be written more nicely. I avoided making the list of die rolls an infinitely long lazy list in case we wanted to use the same RNG for other stuff too
Okay, since you've already done his homework for him... :-) main = do r <- newStdGen print (dice 4 r) dice :: Integer -> StdGen -> [Integer] dice n g = take n $ randomRs (1,6) g -kzm -- If I haven't seen further, it is by standing in the footprints of giants

On 17 Dec 2002, Ketil Z. Malde wrote: (snip)
dice :: Integer -> StdGen -> [Integer] dice n g = take n $ randomRs (1,6) g
Can we still do this concisely and get the new state of the rng back out the other end after the die has been thrown a few times? Or are things like newStdGen meant to be so cheap that it's fine to use lots of different RNGs instead of one that you thread through everything? -- Mark

Mark Carroll writes:
On 17 Dec 2002, Ketil Z. Malde wrote: (snip)
dice :: Integer -> StdGen -> [Integer] dice n g = take n $ randomRs (1,6) g
Can we still do this concisely and get the new state of the rng back out the other end after the die has been thrown a few times?
Oops; I missed that part!
Or are things like newStdGen meant to be so cheap that it's fine to use lots of different RNGs instead of one that you thread through everything?
I've no idea - I've always used StdGen's as if they were going out of style. (You can, of course, `split` them and get two for the price of one) -kzm -- If I haven't seen further, it is by standing in the footprints of giants

On 17 Dec 2002, Ketil Z. Malde wrote:
Mark Carroll writes: (snip)
Can we still do this concisely and get the new state of the rng back out the other end after the die has been thrown a few times?
Oops; I missed that part!
No problem - it wasn't exactly clearly part of the original problem specification. (-: It was good to see what randomRs does, too.
Or are things like newStdGen meant to be so cheap that it's fine to use lots of different RNGs instead of one that you thread through everything?
Also, I was wondering if I can or should use monads to thread the RNG state through everything instead of always returning these two-tuples; I've been peering at things like Control.Monad.Cont to try to see what they're good for.
I've no idea - I've always used StdGen's as if they were going out of style. (You can, of course, `split` them and get two for the price of one)
Ah - I was never sure what to make of that - I normally just use the GHC online Haddockised stuff which tells me no more than the type signatures, but I suppose "split" must be more than (\x->(x,x))! (-: (I'll be happy to help with adding documentation once I'm sure of the semantics myself.) -- Mark

Mark Carroll writes:
On 17 Dec 2002, Ketil Z. Malde wrote:
Ah - I was never sure what to make of that - I normally just use the GHC online Haddockised stuff which tells me no more than the type signatures, but I suppose "split" must be more than (\x->(x,x))!
Well, as SPJ said, you can browse the library report. Basically, it splits a random generator, so that you can pass it to a function and forget about it. -kzm -- If I haven't seen further, it is by standing in the footprints of giants
participants (3)
-
Filipe Santos
-
ketil@ii.uib.no
-
Mark Carroll