Skip to content

Instantly share code, notes, and snippets.

@deque-blog
Last active February 4, 2017 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deque-blog/9043d9a077f0a7c051cc921fcf3804bf to your computer and use it in GitHub Desktop.
Save deque-blog/9043d9a077f0a7c051cc921fcf3804bf to your computer and use it in GitHub Desktop.
perturb :: (Integral n) => n -> StdGen -> StdGen
perturb n rand0 =
foldl
(\rand b -> vary b rand) -- Vary generator based on digit value
(vary (n < 0) rand0) -- Vary generator based on sign
(digits (abs n)) -- Decompose a positive number in digits
where
vary digit rand =
(if digit then snd else fst)
(split rand)
digits =
map ((== 0) . (`mod` 2))
. takeWhile (> 0)
. iterate (`div` 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment