mersenne-random-pure64-0.2.0.3/0000755000175000000120000000000011340314334014702 5ustar donswheelmersenne-random-pure64-0.2.0.3/System/0000755000175000000120000000000011340314334016166 5ustar donswheelmersenne-random-pure64-0.2.0.3/System/Random/0000755000175000000120000000000011340314334017406 5ustar donswheelmersenne-random-pure64-0.2.0.3/System/Random/Mersenne/0000755000175000000120000000000011340314334021162 5ustar donswheelmersenne-random-pure64-0.2.0.3/System/Random/Mersenne/Pure64/0000755000175000000120000000000011340314334022247 5ustar donswheelmersenne-random-pure64-0.2.0.3/System/Random/Mersenne/Pure64/MTBlock.hs0000644000175000000120000000561711340314334024107 0ustar donswheel{-# LANGUAGE MagicHash, UnboxedTuples, BangPatterns #-} -------------------------------------------------------------------- -- | -- Module : System.Random.Mersenne.Pure64 -- Copyright : Copyright (c) 2008, Bertram Felgenhauer -- License : BSD3 -- Maintainer : Don Stewart -- Stability : experimental -- Portability: -- Tested with: GHC 6.8.3 -- -- A purely functional binding 64 bit binding to the classic mersenne -- twister random number generator. This is more flexible than the -- impure 'mersenne-random' library, at the cost of being a bit slower. -- This generator is however, many times faster than System.Random, -- and yields high quality randoms with a long period. -- module System.Random.Mersenne.Pure64.MTBlock ( -- * Block type MTBlock, -- * Block functions seedBlock, nextBlock, lookupBlock, -- * Misc functions blockLen, mixWord64, ) where import GHC.Exts import GHC.IOBase import GHC.Word import System.Random.Mersenne.Pure64.Base data MTBlock = MTBlock ByteArray# allocateBlock :: IO MTBlock allocateBlock = IO $ \s0 -> case newPinnedByteArray# blockSize# s0 of (# s1, b0 #) -> case unsafeFreezeByteArray# b0 s1 of (# s2, b1 #) -> (# s2, MTBlock b1 #) where !(I# blockSize#) = blockSize blockAsPtr :: MTBlock -> Ptr a blockAsPtr (MTBlock b) = Ptr (byteArrayContents# b) -- | create a new MT block, seeded with the given Word64 value seedBlock :: Word64 -> MTBlock seedBlock seed = unsafeDupablePerformIO $ do b <- allocateBlock c_seed_genrand64_block (blockAsPtr b) seed c_next_genrand64_block (blockAsPtr b) (blockAsPtr b) touch b return b {-# NOINLINE seedBlock #-} -- | step: create a new MTBlock buffer from the previous one nextBlock :: MTBlock -> MTBlock nextBlock b = unsafeDupablePerformIO $ do new <- allocateBlock c_next_genrand64_block (blockAsPtr b) (blockAsPtr new) touch b touch new return new {-# NOINLINE nextBlock #-} -- stolen from GHC.ForeignPtr - make sure the argument is still alive. touch :: a -> IO () touch r = IO $ \s0 -> case touch# r s0 of s1 -> (# s1, () #) -- | look up an element of an MT block lookupBlock :: MTBlock -> Int -> Word64 lookupBlock (MTBlock b) (I# i) = W64# (indexWord64Array# b i) -- | MT's word mix function. -- -- (MT applies this function to each Word64 from the buffer before returning it) mixWord64 :: Word64 -> Word64 mixWord64 = c_mix_word64 -- Alternative implementation - it's probably faster on 64 bit machines, but -- on Athlon XP it loses. {- mixWord64 (W64# x0) = let W64# x1 = W64# x0 `xor` (W64# (x0 `uncheckedShiftRL64#` 28#) .&. 0x5555555555555555) W64# x2 = W64# x1 `xor` (W64# (x1 `uncheckedShiftL64#` 17#) .&. 0x71D67FFFEDA60000) W64# x3 = W64# x2 `xor` (W64# (x2 `uncheckedShiftL64#` 37#) .&. 0xFFF7EEE000000000) W64# x4 = W64# x3 `xor` (W64# (x3 `uncheckedShiftRL64#` 43#)) in W64# x4 -} mersenne-random-pure64-0.2.0.3/System/Random/Mersenne/Pure64/Base.hsc0000644000175000000120000000472311340314334023626 0ustar donswheel{-# LANGUAGE EmptyDataDecls, CPP, ForeignFunctionInterface #-} -------------------------------------------------------------------- -- | -- Module : System.Random.Mersenne.Pure64.Base -- Copyright : Copyright (c) 2008, Don Stewart -- License : BSD3 -- Maintainer : Don Stewart -- Stability : experimental -- Portability: CPP, FFI, EmptyDataDecls -- Tested with: GHC 6.8.3 -- -- A purely functional binding 64 bit binding to the classic mersenne -- twister random number generator. This is more flexible than the -- impure 'mersenne-random' library, at the cost of being a bit slower. -- This generator is however, many times faster than System.Random, -- and yields high quality randoms with a long period. -- module System.Random.Mersenne.Pure64.Base where #include "mt19937-64-block.h" #include "mt19937-64.h" #include "mt19937-64-unsafe.h" import Foreign.C.Types import Foreign data MTState type UInt64 = CULLong ------------------------------------------------------------------------ -- pure version: foreign import ccall unsafe "init_genrand64" c_init_genrand64 :: Ptr MTState -> UInt64 -> IO () foreign import ccall unsafe "genrand64_int64" c_genrand64_int64 :: Ptr MTState -> IO UInt64 foreign import ccall unsafe "genrand64_real2" c_genrand64_real2 :: Ptr MTState -> IO CDouble sizeof_MTState :: Int sizeof_MTState = (# const sizeof (struct mt_state_t) ) -- 2504 bytes ------------------------------------------------------------------------ -- block based version: foreign import ccall unsafe "mix_bits" c_mix_word64 :: Word64 -> Word64 foreign import ccall unsafe "seed_genrand64_block" c_seed_genrand64_block :: Ptr a -> Word64 -> IO () foreign import ccall unsafe "next_genrand64_block" c_next_genrand64_block :: Ptr a -> Ptr a -> IO () -- | length of an MT block blockLen :: Int blockLen = (# const NN ) -- | size of an MT block, in bytes blockSize :: Int blockSize = (# const sizeof (mt_block_struct) ) ------------------------------------------------------------------------ -- model: (for testing purposes) foreign import ccall unsafe "init_genrand64_unsafe" c_init_genrand64_unsafe :: UInt64 -> IO () foreign import ccall unsafe "genrand64_int64_unsafe" c_genrand64_int64_unsafe :: IO UInt64 foreign import ccall unsafe "genrand64_real2_unsafe" c_genrand64_real2_unsafe :: IO CDouble foreign import ccall unsafe "string.h memcpy" c_memcpy :: Ptr Word8 -> Ptr Word8 -> CSize -> IO (Ptr Word8) mersenne-random-pure64-0.2.0.3/System/Random/Mersenne/Pure64.hs0000644000175000000120000001014011340314334022577 0ustar donswheel{-# LANGUAGE CPP, ForeignFunctionInterface #-} -------------------------------------------------------------------- -- | -- Module : System.Random.Mersenne.Pure64 -- Copyright : Copyright (c) 2008, Don Stewart -- License : BSD3 -- Maintainer : Don Stewart -- Stability : experimental -- Portability: CPP, FFI -- Tested with: GHC 6.8.3 -- -- A purely functional binding 64 bit binding to the classic mersenne -- twister random number generator. This is more flexible than the -- impure 'mersenne-random' library, at the cost of being a bit slower. -- This generator is however, many times faster than System.Random, -- and yields high quality randoms with a long period. -- -- This generator may be used with System.Random, however, that is -- likely to be slower than using it directly. -- module System.Random.Mersenne.Pure64 ( -- * The random number generator PureMT -- abstract: RandomGen -- * Introduction , pureMT -- :: Word64 -> PureMT , newPureMT -- :: IO PureMT -- $instance -- * Low level access to the generator -- $notes , randomInt -- :: PureMT -> (Int ,PureMT) , randomWord -- :: PureMT -> (Word ,PureMT) , randomInt64 -- :: PureMT -> (Int64 ,PureMT) , randomWord64 -- :: PureMT -> (Word64,PureMT) , randomDouble -- :: PureMT -> (Double,PureMT) ) where ------------------------------------------------------------------------ import System.Random.Mersenne.Pure64.MTBlock import System.Random import Data.Word import Data.Int import System.Time import System.CPUTime -- | Create a PureMT generator from a 'Word64' seed. pureMT :: Word64 -> PureMT pureMT = mkPureMT . seedBlock . fromIntegral -- | Create a new PureMT generator, using the clocktime as the base for the seed. newPureMT :: IO PureMT newPureMT = do ct <- getCPUTime (TOD sec psec) <- getClockTime return $ pureMT (fromIntegral $ sec * 1013904242 + psec + ct) ------------------------------------------------------------------------ -- System.Random interface. -- $instance -- -- Being purely functional, the PureMT generator is an instance of -- RandomGen. However, it doesn't support 'split' yet. instance RandomGen PureMT where next = randomInt split = error "System.Random.Mersenne.Pure: unable to split the mersenne twister" ------------------------------------------------------------------------ -- Direct access to Int, Word and Double types -- | Yield a new 'Int' value from the generator, returning a new -- generator and that 'Int'. The full 64 bits will be used on a 64 bit machine. randomInt :: PureMT -> (Int,PureMT) randomInt g = (fromIntegral i, g') where (i, g') = randomWord64 g {-# INLINE randomInt #-} -- | Yield a new 'Word' value from the generator, returning a new -- generator and that 'Word'. randomWord :: PureMT -> (Word,PureMT) randomWord g = (fromIntegral i, g') where (i, g') = randomWord64 g {-# INLINE randomWord #-} -- | Yield a new 'Int64' value from the generator, returning a new -- generator and that 'Int64'. randomInt64 :: PureMT -> (Int64,PureMT) randomInt64 g = (fromIntegral i, g') where (i, g') = randomWord64 g {-# INLINE randomInt64 #-} -- | Efficiently yield a new 53-bit precise 'Double' value, and a new generator. randomDouble :: PureMT -> (Double,PureMT) randomDouble g = (fromIntegral (i `div` 2048) / 9007199254740992, g') where (i, g') = randomWord64 g {-# INLINE randomDouble #-} -- | Yield a new 'Word64' value from the generator, returning a new -- generator and that 'Word64'. randomWord64 :: PureMT -> (Word64,PureMT) randomWord64 (PureMT block i nxt) = (mixWord64 (block `lookupBlock` i), mt) where mt | i < blockLen-1 = PureMT block (i+1) nxt | otherwise = mkPureMT nxt {-# INLINE randomWord64 #-} -- | 'PureMT', a pure mersenne twister pseudo-random number generator -- data PureMT = PureMT {-# UNPACK #-} !MTBlock {-# UNPACK #-} !Int MTBlock instance Show PureMT where show _ = show "" -- create a new PureMT from an MTBlock mkPureMT :: MTBlock -> PureMT mkPureMT block = PureMT block 0 (nextBlock block) mersenne-random-pure64-0.2.0.3/LICENSE.mt19937-640000644000175000000120000000461411340314334017177 0ustar donswheel/* A C-program for MT19937-64 (2004/9/29 version). Coded by Takuji Nishimura and Makoto Matsumoto. This is a 64-bit version of Mersenne Twister pseudorandom number generator. Before using, initialize the state by using init_genrand64(seed) or init_by_array64(init_key, key_length). Copyright (C) 2004, Makoto Matsumoto and Takuji Nishimura, All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. References: T. Nishimura, ``Tables of 64-bit Mersenne Twisters'' ACM Transactions on Modeling and Computer Simulation 10. (2000) 348--357. M. Matsumoto and T. Nishimura, ``Mersenne Twister: a 623-dimensionally equidistributed uniform pseudorandom number generator'' ACM Transactions on Modeling and Computer Simulation 8. (Jan. 1998) 3--30. Any feedback is very welcome. http://www.math.hiroshima-u.ac.jp/~m-mat/MT/emt.html email: m-mat @ math.sci.hiroshima-u.ac.jp (remove spaces) */ mersenne-random-pure64-0.2.0.3/TODO0000644000175000000120000000132311340314334015371 0ustar donswheel-fvia-C seems to break things! The problem is that the prototypes of the imported functions don't get propagated properly, leading to warnings like this: /tmp/ghc7602_0/ghc7602_0.hc:718:0: warning: implicit declaration of function 'mix_bits' This is actually an error on 32 bit machines - the default return type int is not large enough to hold the 64 bit result. This is fixed in ghc 6.9; the compiler emits its own prototypes. Check sequences are repeatable Investigate slow conversion to Double (x86 at least) looking at the core, the conversion goes through toInteger and encodeFloat. That's a pity, because the FPU has an instruction for this purpose. profile/benchmark tests driver mersenne-random-pure64-0.2.0.3/include/0000755000175000000120000000000011340314334016325 5ustar donswheelmersenne-random-pure64-0.2.0.3/include/mt19937-64-unsafe.h0000644000175000000120000000525711340314334021252 0ustar donswheel/* A C-program for MT19937-64 (2004/9/29 version). Coded by Takuji Nishimura and Makoto Matsumoto. This is a 64-bit version of Mersenne Twister pseudorandom number generator. Before using, initialize the state by using init_genrand64(seed) or init_by_array64(init_key, key_length). Copyright (C) 2004, Makoto Matsumoto and Takuji Nishimura, All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. References: T. Nishimura, ``Tables of 64-bit Mersenne Twisters'' ACM Transactions on Modeling and Computer Simulation 10. (2000) 348--357. M. Matsumoto and T. Nishimura, ``Mersenne Twister: a 623-dimensionally equidistributed uniform pseudorandom number generator'' ACM Transactions on Modeling and Computer Simulation 8. (Jan. 1998) 3--30. Any feedback is very welcome. http://www.math.hiroshima-u.ac.jp/~m-mat/MT/emt.html email: m-mat @ math.sci.hiroshima-u.ac.jp (remove spaces) */ /* initializes mt[NN] with a seed */ void init_genrand64_unsafe(unsigned long long seed); /* generates a random number on [0, 2^64-1]-interval */ unsigned long long genrand64_int64_unsafe(void); /* generates a random number on [0,1)-real-interval */ double genrand64_real2_unsafe(void); mersenne-random-pure64-0.2.0.3/include/mt19937-64.h0000644000175000000120000000574411340314334017774 0ustar donswheel/* A C-program for MT19937-64 (2004/9/29 version). Coded by Takuji Nishimura and Makoto Matsumoto. This is a 64-bit version of Mersenne Twister pseudorandom number generator. Before using, initialize the state by using init_genrand64(seed) or init_by_array64(init_key, key_length). Copyright (C) 2004, Makoto Matsumoto and Takuji Nishimura, All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. References: T. Nishimura, ``Tables of 64-bit Mersenne Twisters'' ACM Transactions on Modeling and Computer Simulation 10. (2000) 348--357. M. Matsumoto and T. Nishimura, ``Mersenne Twister: a 623-dimensionally equidistributed uniform pseudorandom number generator'' ACM Transactions on Modeling and Computer Simulation 8. (Jan. 1998) 3--30. Any feedback is very welcome. http://www.math.hiroshima-u.ac.jp/~m-mat/MT/emt.html email: m-mat @ math.sci.hiroshima-u.ac.jp (remove spaces) */ #define NN 312 #define MM 156 #define MATRIX_A 0xB5026F5AA96619E9ULL #define UM 0xFFFFFFFF80000000ULL /* Most significant 33 bits */ #define LM 0x7FFFFFFFULL /* Least significant 31 bits */ /* The array for the state vector */ typedef struct mt_state_t { unsigned long long mt[NN]; int mti; } mt_state_struct; typedef struct mt_state_t *mt_state; /* initializes mt[NN] with a seed */ void init_genrand64(mt_state st, unsigned long long seed); /* generates a random number on [0, 2^64-1]-interval */ unsigned long long genrand64_int64(mt_state st); double genrand64_real2(mt_state st); mersenne-random-pure64-0.2.0.3/include/mt19937-64-block.h0000644000175000000120000000571011340314334021055 0ustar donswheel/* A C-program for MT19937-64 (2004/9/29 version). Coded by Takuji Nishimura and Makoto Matsumoto. This is a 64-bit version of Mersenne Twister pseudorandom number generator. Before using, initialize the state by using init_genrand64(seed) or init_by_array64(init_key, key_length). Copyright (C) 2004, Makoto Matsumoto and Takuji Nishimura, All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. References: T. Nishimura, ``Tables of 64-bit Mersenne Twisters'' ACM Transactions on Modeling and Computer Simulation 10. (2000) 348--357. M. Matsumoto and T. Nishimura, ``Mersenne Twister: a 623-dimensionally equidistributed uniform pseudorandom number generator'' ACM Transactions on Modeling and Computer Simulation 8. (Jan. 1998) 3--30. Any feedback is very welcome. http://www.math.hiroshima-u.ac.jp/~m-mat/MT/emt.html email: m-mat @ math.sci.hiroshima-u.ac.jp (remove spaces) */ #define NN 312 #define MM 156 #define MATRIX_A 0xB5026F5AA96619E9ULL #define UM 0xFFFFFFFF80000000ULL /* Most significant 33 bits */ #define LM 0x7FFFFFFFULL /* Least significant 31 bits */ /* The array for the state vector */ typedef struct mt_block_t { unsigned long long mt[NN]; } mt_block_struct, *mt_block; /* initializes mt[NN] with a seed */ void seed_genrand64_block(mt_block st, unsigned long long seed); /* calculate new block of random data */ void next_genrand64_block(mt_block st, mt_block newst); unsigned long long mix_bits(unsigned long long x); mersenne-random-pure64-0.2.0.3/cbits/0000755000175000000120000000000011340314334016006 5ustar donswheelmersenne-random-pure64-0.2.0.3/cbits/mt19937-64.c0000644000175000000120000001007111340314334017435 0ustar donswheel/* A C-program for MT19937-64 (2004/9/29 version). Coded by Takuji Nishimura and Makoto Matsumoto. This is a 64-bit version of Mersenne Twister pseudorandom number generator. Before using, initialize the state by using init_genrand64(seed) or init_by_array64(init_key, key_length). Copyright (C) 2004, Makoto Matsumoto and Takuji Nishimura, All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. References: T. Nishimura, ``Tables of 64-bit Mersenne Twisters'' ACM Transactions on Modeling and Computer Simulation 10. (2000) 348--357. M. Matsumoto and T. Nishimura, ``Mersenne Twister: a 623-dimensionally equidistributed uniform pseudorandom number generator'' ACM Transactions on Modeling and Computer Simulation 8. (Jan. 1998) 3--30. Any feedback is very welcome. http://www.math.hiroshima-u.ac.jp/~m-mat/MT/emt.html email: m-mat @ math.sci.hiroshima-u.ac.jp (remove spaces) */ #include #include #include "mt19937-64.h" /* * When passed an empty state, initialize the states' mt[NN] with a seed */ void init_genrand64(mt_state st, unsigned long long seed) { st->mti=NN+1; st->mt[0] = seed; for (st->mti=1; st->mtimti++) st->mt[st->mti] = (6364136223846793005ULL * (st->mt[st->mti-1] ^ (st->mt[st->mti-1] >> 62)) + st->mti); } /* generates a random number on [0, 2^64-1]-interval */ unsigned long long genrand64_int64(mt_state st) { int i; unsigned long long x; static unsigned long long mag01[2]={0ULL, MATRIX_A}; if (st->mti >= NN) { /* generate NN words at one time */ /* if init_genrand64() has not been called, */ /* a default initial seed is used */ if (st->mti == NN+1) init_genrand64(st, 5489ULL); for (i=0;imt[i]&UM)|(st->mt[i+1]&LM); st->mt[i] = st->mt[i+MM] ^ (x>>1) ^ mag01[(int)(x&1ULL)]; } for (;imt[i]&UM)|(st->mt[i+1]&LM); st->mt[i] = st->mt[i+(MM-NN)] ^ (x>>1) ^ mag01[(int)(x&1ULL)]; } x = (st->mt[NN-1]&UM)|(st->mt[0]&LM); st->mt[NN-1] = st->mt[MM-1] ^ (x>>1) ^ mag01[(int)(x&1ULL)]; st->mti = 0; } x = st->mt[st->mti++]; x ^= (x >> 29) & 0x5555555555555555ULL; x ^= (x << 17) & 0x71D67FFFEDA60000ULL; x ^= (x << 37) & 0xFFF7EEE000000000ULL; x ^= (x >> 43); return x; } /* generates a random number on [0,1)-real-interval */ double genrand64_real2(mt_state st) { return (genrand64_int64(st) >> 11) * (1.0/9007199254740992.0); } mersenne-random-pure64-0.2.0.3/cbits/mt19937-64-unsafe.c0000644000175000000120000001025611340314334020721 0ustar donswheel/* A C-program for MT19937-64 (2004/9/29 version). Coded by Takuji Nishimura and Makoto Matsumoto. This is a 64-bit version of Mersenne Twister pseudorandom number generator. Before using, initialize the state by using init_genrand64(seed) or init_by_array64(init_key, key_length). Copyright (C) 2004, Makoto Matsumoto and Takuji Nishimura, All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. References: T. Nishimura, ``Tables of 64-bit Mersenne Twisters'' ACM Transactions on Modeling and Computer Simulation 10. (2000) 348--357. M. Matsumoto and T. Nishimura, ``Mersenne Twister: a 623-dimensionally equidistributed uniform pseudorandom number generator'' ACM Transactions on Modeling and Computer Simulation 8. (Jan. 1998) 3--30. Any feedback is very welcome. http://www.math.hiroshima-u.ac.jp/~m-mat/MT/emt.html email: m-mat @ math.sci.hiroshima-u.ac.jp (remove spaces) */ #include #include "mt19937-64-unsafe.h" #define NN 312 #define MM 156 #define MATRIX_A 0xB5026F5AA96619E9ULL #define UM 0xFFFFFFFF80000000ULL /* Most significant 33 bits */ #define LM 0x7FFFFFFFULL /* Least significant 31 bits */ /* The array for the state vector */ static unsigned long long mt[NN]; /* mti==NN+1 means mt[NN] is not initialized */ static int mti=NN+1; /* initializes mt[NN] with a seed */ void init_genrand64_unsafe(unsigned long long seed) { mt[0] = seed; for (mti=1; mti> 62)) + mti); } /* generates a random number on [0, 2^64-1]-interval */ unsigned long long genrand64_int64_unsafe(void) { int i; unsigned long long x; static unsigned long long mag01[2]={0ULL, MATRIX_A}; if (mti >= NN) { /* generate NN words at one time */ /* if init_genrand64() has not been called, */ /* a default initial seed is used */ if (mti == NN+1) init_genrand64_unsafe(5489ULL); for (i=0;i>1) ^ mag01[(int)(x&1ULL)]; } for (;i>1) ^ mag01[(int)(x&1ULL)]; } x = (mt[NN-1]&UM)|(mt[0]&LM); mt[NN-1] = mt[MM-1] ^ (x>>1) ^ mag01[(int)(x&1ULL)]; mti = 0; } x = mt[mti++]; x ^= (x >> 29) & 0x5555555555555555ULL; x ^= (x << 17) & 0x71D67FFFEDA60000ULL; x ^= (x << 37) & 0xFFF7EEE000000000ULL; x ^= (x >> 43); return x; } /* generates a random number on [0,1)-real-interval */ double genrand64_real2_unsafe(void) { return (genrand64_int64_unsafe() >> 11) * (1.0/9007199254740992.0); } mersenne-random-pure64-0.2.0.3/cbits/mt19937-64-block.c0000644000175000000120000000720011340314334020525 0ustar donswheel/* A C-program for MT19937-64 (2004/9/29 version). Coded by Takuji Nishimura and Makoto Matsumoto. This is a 64-bit version of Mersenne Twister pseudorandom number generator. Copyright (C) 2004, Makoto Matsumoto and Takuji Nishimura, All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. References: T. Nishimura, ``Tables of 64-bit Mersenne Twisters'' ACM Transactions on Modeling and Computer Simulation 10. (2000) 348--357. M. Matsumoto and T. Nishimura, ``Mersenne Twister: a 623-dimensionally equidistributed uniform pseudorandom number generator'' ACM Transactions on Modeling and Computer Simulation 8. (Jan. 1998) 3--30. Any feedback is very welcome. http://www.math.hiroshima-u.ac.jp/~m-mat/MT/emt.html email: m-mat @ math.sci.hiroshima-u.ac.jp (remove spaces) Modified by Don Stewart to use non-global state. Modified by Bertram Felgenhauer to provide block oriented functions only. */ #include #include #include "mt19937-64-block.h" /* * When passed an empty state, initialize the states' mt[NN] with a seed */ void seed_genrand64_block(mt_block st, unsigned long long seed) { int i; st->mt[0] = seed; for (i=1; imt[i] = (6364136223846793005ULL * (st->mt[i-1] ^ (st->mt[i-1] >> 62)) + i); } /* generates a new state buffer from the previous one */ void next_genrand64_block(mt_block st, mt_block newst) { int i; unsigned long long x; static unsigned long long mag01[2]={0ULL, MATRIX_A}; for (i=0; imt[i]&UM)|(st->mt[i+1]&LM); newst->mt[i] = st->mt[i+MM] ^ (x>>1) ^ mag01[(int)(x&1ULL)]; } for (; imt[i]&UM)|(st->mt[i+1]&LM); newst->mt[i] = newst->mt[i+(MM-NN)] ^ (x>>1) ^ mag01[(int)(x&1ULL)]; } x = (st->mt[NN-1]&UM)|(newst->mt[0]&LM); newst->mt[NN-1] = newst->mt[MM-1] ^ (x>>1) ^ mag01[(int)(x&1ULL)]; } unsigned long long mix_bits(unsigned long long x) { x ^= (x >> 29) & 0x5555555555555555ULL; x ^= (x << 17) & 0x71D67FFFEDA60000ULL; x ^= (x << 37) & 0xFFF7EEE000000000ULL; x ^= (x >> 43); return x; } mersenne-random-pure64-0.2.0.3/Setup.lhs0000644000175000000120000000011411340314334016506 0ustar donswheel#!/usr/bin/env runhaskell > import Distribution.Simple > main = defaultMain mersenne-random-pure64-0.2.0.3/LICENSE0000644000175000000120000000270011340314334015706 0ustar donswheelCopyright (c) Don Stewart 2008 All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the author nor the names of his contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. mersenne-random-pure64-0.2.0.3/mersenne-random-pure64.cabal0000644000175000000120000000432011340314334022102 0ustar donswheelname: mersenne-random-pure64 version: 0.2.0.3 homepage: http://code.haskell.org/~dons/code/mersenne-random-pure64/ synopsis: Generate high quality pseudorandom numbers purely using a Mersenne Twister description: The Mersenne twister is a pseudorandom number generator developed by Makoto Matsumoto and Takuji Nishimura that is based on a matrix linear recurrence over a finite binary field. It provides for fast generation of very high quality pseudorandom numbers. The source for the C code can be found here: . . This library provides a purely functional binding to the 64 bit classic mersenne twister, along with instances of RandomGen, so the generator can be used with System.Random. The generator should typically be a few times faster than the default StdGen (but a tad slower than the impure 'mersenne-random' library based on SIMD instructions and destructive state updates. . category: Math, System license: BSD3 license-file: LICENSE copyright: (c) 2008. Don Stewart author: Don Stewart maintainer: Don Stewart cabal-version: >= 1.2.0 build-type: Simple tested-with: GHC == 6.8.3 flag small_base description: Build with new smaller base library default: False library exposed-modules: System.Random.Mersenne.Pure64 System.Random.Mersenne.Pure64.Base System.Random.Mersenne.Pure64.MTBlock extensions: CPP, ForeignFunctionInterface if flag(small_base) build-depends: base < 3 else build-depends: base >= 3 && < 6, old-time, random cc-options: -O3 -finline-functions -fomit-frame-pointer -fno-strict-aliasing --param max-inline-insns-single=1800 ghc-options: -Wall -O2 -fexcess-precision c-sources: cbits/mt19937-64.c cbits/mt19937-64-unsafe.c cbits/mt19937-64-block.c include-dirs: include includes: install-includes: mt19937-64.h mt19937-64-unsafe.h mt19937-64-block.h mersenne-random-pure64-0.2.0.3/tests/0000755000175000000120000000000011340314334016044 5ustar donswheelmersenne-random-pure64-0.2.0.3/tests/Unit.hs0000644000175000000120000001073511340314334017325 0ustar donswheel{-# LANGUAGE BangPatterns #-} -- A basic correctness and performance test for mersenne-random-pure64. -- -- Copyright (c) 2008, Don Stewart import Control.Exception import Control.Monad import Data.Int import Data.Typeable import Data.Word import System.CPUTime import System.Environment import System.IO import Text.Printf import qualified System.Random as Old import qualified System.Random.Mersenne as Unsafe import System.Random.Mersenne.Pure64 import System.Random.Mersenne.Pure64.Base import Control.Concurrent import Control.Concurrent.MVar time :: IO t -> IO t time a = do start <- getCPUTime v <- a end <- getCPUTime let diff = (fromIntegral (end - start)) / (10^12) printf "Computation time: %0.3f sec\n" (diff :: Double) return v seed = 7 main = do c_init_genrand64_unsafe seed let g = pureMT (fromIntegral seed) ------------------------------------------------------------------------ -- calibrate s <- newMVar 0 :: IO (MVar Int) putStr "Calibrating ... " >> hFlush stdout tid <- forkIO $ do let go !i !g = do let (!_, !g') = randomWord64 g x <- swapMVar s i x `seq` go (i+1) g' go 0 g threadDelay (1000 * 1000) killThread tid lim <- readMVar s -- 1 sec worth of generation putStrLn $ "done. Using N=" ++ show lim time $ do let m = 2*lim putStr $ "Checking against released mt19937-64.c to depth " ++ show m ++ " " hFlush stdout equivalent g m speed lim return () ------------------------------------------------------------------------ equivalent !g !n | n > 0 = do i' <- c_genrand64_int64_unsafe d' <- c_genrand64_real2_unsafe let (i, g') = randomWord64 g (d, g'') = randomDouble g' if i == fromIntegral i' && d == realToFrac d' then do when (n `rem` 500000 == 0) $ putChar '.' >> hFlush stdout equivalent g'' (n-1) else do print $ "Failed! " ++ show ((i,i') , (d,d')) return g'' equivalent g _ = do putStrLn "Matches model!" return g ------------------------------------------------------------------------ -- compare with System.Random -- overhead cause by random's badness speed lim = do time $ do putStrLn $ "System.Random" let g = Old.mkStdGen 5 let go :: Old.StdGen -> Int -> Int -> Int go !g !n !acc | n >= lim = acc | otherwise = let (a, g') = Old.random g in go g' (n+1) (if a > acc then a else acc) print (go g 0 0) time $ do putStrLn $ "System.Random with our generator" let g = pureMT 5 let go :: PureMT -> Int -> Int -> Int go !g !n !acc | n >= lim = acc | otherwise = let (a,g') = Old.random g in go g' (n+1) (if a > acc then a else acc) print (go g 0 0) time $ do putStrLn $ "System.Random.Mersenne.Pure" let g = pureMT 5 let go :: PureMT -> Int -> Int -> Int go !g !n !acc | n >= lim = acc | otherwise = let (a',g') = randomWord64 g a = fromIntegral a' in go g' (n+1) (if a > acc then a else acc) print (go g 0 0) time $ do putStrLn $ "System.Random.Mersenne.Pure generating Double" let g = pureMT 5 let go :: PureMT -> Int -> Double -> Double go !g !n !acc | n >= lim = acc | otherwise = let (a, g') = randomDouble g in go g' (n+1) (if a > acc then a else acc) print (go g 0 0) time $ do putStrLn $ "System.Random.Mersenne.Pure (unique state)" c_init_genrand64_unsafe 5 let go :: Int -> Int -> IO Int go !n !acc | n >= lim = return acc | otherwise = do a' <- c_genrand64_int64_unsafe let a = fromIntegral a' go (n+1) (if a > acc then a else acc) print =<< go 0 0 time $ do putStrLn $ "System.Random.Mersenne.Unsafe" g <- Unsafe.newMTGen (Just 5) let go :: Int -> Int -> IO Int go !n !acc | n >= lim = return acc | otherwise = do a <- Unsafe.random g go (n+1) (if a > acc then a else acc) print =<< go 0 0 -- printf "MT is %s times faster generating %s\n" (show $x`div`y) (show (typeOf ty)) -- return () mersenne-random-pure64-0.2.0.3/tests/copy.hs0000644000175000000120000000044211340314334017352 0ustar donswheel{-# LANGUAGE BangPatterns #-} import System.Random.Mersenne.Pure64 main = do let g = pureMT 7 let go :: Int -> PureMT -> IO () go 0 !p = return () go n !p = do let (_,q) = randomWord p go (n-1) q go 10000000 g -- 10s constant space