[xmonad] question about code in XMonad.Actions.GridSelect

Platon Pronko platon7pronko at gmail.com
Thu Jul 15 05:44:06 UTC 2021


Hi!

I'm looking at XMonad.Actions.GridSelect code (wanted to fix compilation warnings), and I stumbled over the following function.

-- | Generates a Double from a string, trying to
-- achieve a random distribution.
-- We create a random seed from the sum of all characters
-- in the string, and use it to generate a ratio between 0 and 1
stringToRatio :: String -> Double
stringToRatio "" = 0
stringToRatio s = let gen = mkStdGen $ sum $ map fromEnum s
                       range = (\(a, b) -> b - a) $ genRange gen
                       randomInt = foldr1 combine $ replicate 20 next
                       combine f1 f2 g = let (_, g') = f1 g in f2 g'
                   in fi (fst $ randomInt gen) / fi range

Looks like it is supposed to simply generate a Double number between 0 and 1 - why such complicated approach was used instead of `uniformR` from System.Random?

(also it seems that it is buggy and returns huge negative numbers, because `range = (\(a, b) -> b - a) $ genRange gen` is always -1.

Best regards,
Platon Pronko


More information about the xmonad mailing list