-
Notifications
You must be signed in to change notification settings - Fork 6
/
Random.hs
165 lines (132 loc) · 4.76 KB
/
Random.hs
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
-- some parts are taken from the nhc98/Hugs implementation of Random
module Random (
RandomGen(next, split, genRange),
StdGen, mkStdGen,
Random( random, randomR,
randoms, randomRs,
randomIO, randomRIO ),
getStdRandom, getStdGen, setStdGen, newStdGen
) where
import PreludeBuiltinTypes as NotHat.T
import RandomBuiltin as NotHat.T
import qualified NotHat.System.Random as NotHat.Random
import Char
---------------- The RandomGen class ------------------------
class RandomGen g where
genRange :: g -> (Int, Int)
next :: g -> (Int, g)
split :: g -> (g, g)
-- Default method
genRange g = (minBound,maxBound)
---------------- A standard instance of RandomGen -----------
-- data StdGen = ... -- Abstract
instance RandomGen StdGen where
genRange = primStdGenGenRange
next = primStdGenNext
split = primStdGenSplit
instance Read StdGen where
readsPrec = primStdGenReadsPrec
instance Show StdGen where
showsPrec = primStdGenShowsPrec
foreign import ccall "NotHat.Random.genRange"
primStdGenGenRange :: StdGen -> (Int,Int)
foreign import ccall "NotHat.Random.next"
primStdGenNext :: StdGen -> (Int,StdGen)
foreign import ccall "NotHat.Random.split"
primStdGenSplit :: StdGen -> (StdGen,StdGen)
foreign import ccall "NotHat.Prelude.readsPrec"
primStdGenReadsPrec :: Int -> String -> [(StdGen,String)]
foreign import ccall "NotHat.Prelude.showsPrec"
primStdGenShowsPrec :: Int -> StdGen -> String -> String
foreign import ccall "NotHat.Random.mkStdGen"
mkStdGen :: Int -> StdGen
---------------- The Random class ---------------------------
class Random a where
randomR :: RandomGen g => (a, a) -> g -> (a, g)
random :: RandomGen g => g -> (a, g)
randomRs :: RandomGen g => (a, a) -> g -> [a]
randoms :: RandomGen g => g -> [a]
randomRIO :: (a,a) -> IO a
randomIO :: IO a
-- Default methods
randoms g = x : randoms g'
where
(x,g') = random g
randomRs range g = x : randomRs range g'
where
(x,g') = randomR range g
randomIO = getStdRandom random
randomRIO range = getStdRandom (randomR range)
instance Random Int where
randomR (a,b) g = randomIvalInteger (toInteger a, toInteger b) g
random g = randomR (minBound,maxBound) g
instance Random Integer where
randomR ival g = randomIvalInteger ival g
random g = randomR (toInteger (minBound::Int), toInteger (maxBound::Int)) g
instance Random Float where
random g = randomIvalDouble (0::Double,1) realToFrac g
randomR (a,b) g = randomIvalDouble (realToFrac a, realToFrac b) realToFrac g
instance Random Double where
randomR ival g = randomIvalDouble ival id g
random g = randomR (0::Double,1) g
instance Random Bool where
randomR (a,b) g =
case
(randomIvalInteger (bool2Integer a, bool2Integer b) g) of
(x, g) -> (int2Bool x, g)
where
bool2Integer :: Bool -> Integer
bool2Integer False = 0
bool2Integer True = 1
int2Bool :: Int -> Bool
int2Bool 0 = False
int2Bool _ = True
random g = randomR (minBound,maxBound) g
instance Random Char where
randomR (a,b) g =
case (randomIvalInteger (toInteger (ord a), toInteger (ord b)) g) of
(x,g) -> (chr x, g)
random g = randomR (minBound,maxBound) g
-- internal:
randomIvalInteger :: (RandomGen g, Num a) => (Integer, Integer) -> g -> (a, g)
randomIvalInteger (l,h) rng
| l > h = randomIvalInteger (h,l) rng
| otherwise = case (f n 1 rng) of (v, rng') -> (fromInteger (l + v `mod` k), rng')
where
k = h - l + 1
b = 2147483561
n = iLogBase b k
f 0 acc g = (acc, g)
f n acc g =
let
(x,g') = next g
in
f (n-1) (fromInt x + acc * b) g'
randomIvalDouble :: (RandomGen g, Fractional a) => (Double, Double) -> (Double -> a) -> g -> (a, g)
randomIvalDouble (l,h) fromDouble rng
| l > h = randomIvalDouble (h,l) fromDouble rng
| otherwise =
case (randomIvalInteger (toInteger (minBound::Int), toInteger (maxBound::Int)) rng) of
(x, rng') ->
let
scaled_x =
fromDouble ((l+h)/2) +
fromDouble ((h-l) / realToFrac intRange) *
fromIntegral (x::Int)
in
(scaled_x, rng')
fromInt :: Num a => Int -> a
fromInt = fromInteger . toInteger
intRange :: Integer
intRange = toInteger (maxBound::Int) - toInteger (minBound::Int)
iLogBase :: Integer -> Integer -> Integer
iLogBase b i = if i < b then 1 else 1 + iLogBase b (i `div` b)
---------------- The global random generator ----------------
foreign import ccall "NotHat.Random.newStdGen"
newStdGen :: IO StdGen
foreign import ccall "NotHat.Random.setStdGen"
setStdGen :: StdGen -> IO ()
foreign import ccall "NotHat.Random.getStdGen"
getStdGen :: IO StdGen
foreign import ccall "NotHat.Random.getStdRandom"
getStdRandom :: (StdGen -> (a, StdGen)) -> IO a