I'm running round in circles with the need to generate a random time between two given LocalTime
values (but this can be UTCTime
if necessary, as I can convert).
I achieve the generation of a number in the range (0, n)
like this:
randomSeconds :: RandomGen g => g -> NominalDiffTime -> NominalDiffTime
randomSeconds rg max = realToFrac $ fst (randomR (0, max) rg)
and this is used like so:
randomTimeBetween :: RandomGen g => TimeZone -> LocalTime -> LocalTime -> g -> UTCTime
randomTimeBetween tz s e rg = addUTCTime (randomSeconds rg (diff tz s e)) (localTimeToUTC tz s)
where diff
is
diff :: TimeZone -> LocalTime -> LocalTime -> NominalDiffTime
diff tz st en = diffUTCTime (localTimeToUTC tz st) (localTimeToUTC tz en)
However I'm in the endless cycle of compilation errors where it could not deduce Random NominalDiffTime
or can't convert from NominalDiffTime
because there's no instance for (Fractional Int)
etc
What's the most appropriate/simplest/whatever way of me generating a random NominalDiffTime
so I can then add that amount using addUTCTime x utc
?
Aucun commentaire:
Enregistrer un commentaire