Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 36 lines (30 sloc) 0.979 kb
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

-- |
-- Don't depend on any external source of psuedo-random numbers, because
-- we want to be able to save a psuedo-random seed and know that we can
-- generate the same psuedo-random sequence when we reload it, even across
-- different environments.
--
module Roguestar.Lib.RNG
    (mkRNG,
     RNG,
     Random(..),
     RandomGen(..))
    where

import System.Random
import Control.Arrow (first)

newtype RNG = RNG { rng_state :: Integer }

instance RandomGen RNG where
    next g = (fromInteger $ x `quot` (2^24),RNG x)
        where x = (rng_state g * 0x5DEECE66D + 0xB) `mod` (2^48)
    split g = (mkRNG $ fromIntegral x,mkRNG $ fromIntegral y)
        where (x,g') = next g
              (y,_) = next g'
    genRange _ = (0,2^24)

instance Random RNG where
    random = first (mkRNG :: Integer -> RNG) . random
    randomR _ = random

-- |
-- Construct an RNG from a seed.
--
mkRNG :: (Integral i) => i -> RNG
mkRNG = RNG . fromIntegral . fst . next . RNG . toInteger
Something went wrong with that request. Please try again.