haskell / haskell/random

skip and mix methods in Random typeclass?

Open
#39 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
Haskell
Stars
59
Forks
56
PR merge metrics
No merged PRs in 30d

Description

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:

``` haskell
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:

``` haskell
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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.