[Haskell-cafe] more improvable randomness

Michael Mossey mpm at alumni.caltech.edu
Thu Oct 8 11:38:46 EDT 2009


I wrote some code to model the keystroke-to-keystroke delay in a person
typing, with pseudorandomness. There are two kinds of delays.. one is a
very small delay as the person reaches for a new key (call this 'reach'
delays), the other is a larger delay that represents a pause to think or
to take a break (call this 'break' delays).

My thought is that I could create an infinite series of reach delays and
an infinite series of break delays and zipWith (+).

The inifinite series of break delays would look like:

- The overall pattern is a large number of zeros, in the range (N,M)
followed by a single positive number, chosen from a distribution D... and
then repeat.

Here's some code. I used the Gen monad to gain access to the 'frequency'
function, which is useful, but I had trouble figuring out how to put the
whole algorithm into Gen. I also looked at Rand. The problem is that if I
want to create the list by 'sequence'ing a list of monads, they need to
have state that remembers how many zeros are left to go.



breakSeries :: Int -> Int -> StdGen -> [Float]
breakSeries lowerB upperB gen =
   let (n,gen1) = randomR (lowerB,upperB) gen
       (gen2,gen3) = split gen1
       delay = generate 1 gen2 breakM
   in replicate n 0 ++ [delay] ++ breakSeries lowerB upperB gen3

breakM :: Gen Float
breakM = frequency [ (10, choose( 1::Float   ,  2))
                   , (10, choose( 4::Float   ,  6)) ]

test = (print . take 100 . breakSeries 2 4 ) =<< newStdGen


More information about the Haskell-Cafe mailing list