[hugs-bugs readers: Sorry for the silence last week - I was in Berlin
and came back to find 99 unread messages. Fortunately, most of them
are from the glasgow-haskell-bugs list :-)]
> I have a problem with randomIO, but I don't know if it is bug.
It's a bug.
> For example, if I execute (in Hugs's prompt)
>
> $ randomIO (1,10) >>= \xs ->
> print (take 10 xs)
>
> Hugs returns [8, 8, 1, 6, 4, 7, 1, 10, 8, 9]
>
> so, if I execute it again Hugs returns [6, 5, 4, 3, 8, 6, 10, 2, 7, 9]
>
> Ok, I wanted this result, however if I execute the code that combines two
> randomIO function calls the result is different that I wanted.
>
> I mean,...
>
> $ randomIO (1,10) >>= \xs ->
> randomIO (1,10) >>= \ys ->
> print (take 10 (zip xs ys))
>
> hugs returns
>
> [(9,9), (8,8), (1,1), (9,9), (2,2), (4,4), (8,8), (10,10), (6,6), (3,3)]
>
> I hoped different number in each pair.
> Do you undersantd me? I don't know if it is a bug or is the way that Hugs
> reduces same expresions.
It's caused by the definition of the getRandomSeed primitive used to
define randomIO:
primFun(primGetRandomSeed) { /* generate a random seed */
IOReturn(bigInt(clock()));
}
The problem is that the clock doesn't change very much between the two calls.
Solutions:
1) Get a slower computer :-)
2) Replace the call to the C function "clock" with some other function
such as the random function or the rand function.
Why didn't I do (2) the first time? Dunno. Maybe the person who asked me to
add Random suggested this implementation? Maybe there's some portability
issue?
A