Skip to content

Hierarchical Classes Bounded/Unbounded Random numbers #8

@lehins

Description

@lehins

This discussion has already popped up thanks to @Shimuuar

uniformR says generate all possible values with equal probability. Integer works just fine with this. But uniform? No way! There's infinite number of integers

It would make absolute sense to split number generation into two(three) groups:

  • Bounded: Int, Word, etc:
  • Unbounded: Integer, Natural
    • Potentially also: Float and Double (CFloat and CDouble too of course)
      Reasons I consider including floating point numbers here instead of with fixed width integral types because:
      • There are whole lot of binary representations of floating numbers that aren't really valid: NaN,+/-0 and +/-Infinity
      • Floating point numbers don't have Bounded instance

What are our options:

  1. Leave it broken as it is now in Random and in Variate classes.

  2. Introduce RandomR for all types that can be generated, while using Random only for things that has bounds:

class RandomR a where
  randomRM :: MonadRandom g m => (a, a) -> g -> m a
  randomR :: NoSplitGen g => (a, a) -> g -> (a, g)
  randomR r g = runStateGen g (genRandomR r)

class (Bounded a, RandomR a) => Random a where
  randomM :: MonadRandom g m => g -> m a
  randomM = randomRM (minBound, maxBound)
  random  :: NoSplitGen g => g -> (a, g)
  random g = runStateGen g genRandom

This will be a breaking change for users that are relying on random to generate floating point numbers and unbounded integral types.
Conceptually though, I think this is a good distinction to have.

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions