Skip to content

skip and mix methods in Random typeclass? #39

@Zemyla

Description

@Zemyla

There are RNGs that can skip n values in less than O(n) time, and they should have the opportunity to expose this to the user. And the ones that don't still have a sensible default:

skip :: (RandomGen g) => Int -> g -> g
skip n g | n <= 0 = g
skip n g = case next g of (_, g') -> skip (n - 1) g'

There are also RNGs that allow the user to mix in entropy to the generator, and this also has a sensible default for generators without that operation:

pick :: Bool -> (a, a) -> a
pick False (f, s) = f
pick True (f, s) = s

mix :: (RandomGen g) => Int -> g -> g
mix = go (finiteBitSize (0 :: Int)) where
  go n k g | n `seq` k `seq` g `seq` False = undefined
  go n k g | n < 0 = g
  go n k g = go (n - 1) k (testBit K n `pick` split g)

These methods should be part of the typeclass so they can be available to both consumers of random numbers and those who want to create generators that modify other generators.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions