-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPrng.hs
More file actions
32 lines (26 loc) · 740 Bytes
/
Prng.hs
File metadata and controls
32 lines (26 loc) · 740 Bytes
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
module Prng (
Seed,
Random(..),
makeRandom,
nextDouble,
range,
) where
type Seed = Int
data Random = Random { randomSeed::Seed
, randomA :: Int
, randomM ::Int
} deriving(Show)
makeRandom :: Seed -> Random
makeRandom s = Random s 16807 2147483647
next :: Random -> Random
next (Random s a m) = Random s' a m where s' = (a * s) `mod` m
range :: Random -> Int -> (Int, Random)
range r max = (x, r')
where r' = next r
x = randomSeed r' `mod` max
nextDouble :: Random -> (Double,Random)
nextDouble r = (x,r')
where r' = next r
mf = fromIntegral (randomM r')
seed' = fromIntegral (randomSeed r')
x = seed' / mf