File: Range.hs

package info (click to toggle)
haskell-random 1.2.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 296 kB
  • sloc: haskell: 2,696; makefile: 3
file content (42 lines) | stat: -rw-r--r-- 1,385 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
module Spec.Range
  ( symmetric
  , bounded
  , singleton
  , uniformRangeWithin
  , uniformRangeWithinExcludedF
  , uniformRangeWithinExcludedD
  ) where

import System.Random.Internal
import System.Random.Stateful
import Data.Proxy

symmetric :: (RandomGen g, UniformRange a, Eq a) => Proxy a -> g -> (a, a) -> Bool
symmetric _ g (l, r) = fst (uniformR (l, r) g) == fst (uniformR (r, l) g)

bounded :: (RandomGen g, UniformRange a, Ord a) => Proxy a -> g -> (a, a) -> Bool
bounded _ g (l, r) = bottom <= result && result <= top
  where
    bottom = min l r
    top = max l r
    result = fst (uniformR (l, r) g)

singleton :: (RandomGen g, UniformRange a, Eq a) => Proxy a -> g -> a -> Bool
singleton _ g x = result == x
  where
    result = fst (uniformR (x, x) g)

uniformRangeWithin :: (RandomGen g, UniformRange a, Ord a) => Proxy a -> g -> (a, a) -> Bool
uniformRangeWithin _ gen (l, r) =
  runStateGen_ gen $ \g ->
    (\result -> min l r <= result && result <= max l r) <$> uniformRM (l, r) g

uniformRangeWithinExcludedF :: RandomGen g => g -> Bool
uniformRangeWithinExcludedF gen =
  runStateGen_ gen $ \g ->
    (\result -> 0 < result && result <= 1) <$> uniformFloatPositive01M g

uniformRangeWithinExcludedD :: RandomGen g => g -> Bool
uniformRangeWithinExcludedD gen =
  runStateGen_ gen $ \g ->
    (\result -> 0 < result && result <= 1) <$> uniformDoublePositive01M g