mersenne-random-pure64-0.2.2.0/0000755000000000000000000000000012761037130014254 5ustar0000000000000000mersenne-random-pure64-0.2.2.0/mersenne-random-pure64.cabal0000644000000000000000000000462512761037130021464 0ustar0000000000000000name: mersenne-random-pure64 version: 0.2.2.0 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.6.0 build-type: Simple tested-with: GHC == 7.6.2, GHC == 7.8.4, GHC == 7.10.3, GHC == 8.0.1 source-repository head type: git location: git://github.com/bgamari/mersenne-random-pure64 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 System.Random.Mersenne.Pure64.Internal extensions: CPP, ForeignFunctionInterface if flag(small_base) build-depends: base < 3 else build-depends: base >= 3 && < 6, 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.2.0/LICENSE0000644000000000000000000000270012761037130015260 0ustar0000000000000000Copyright (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.2.0/Setup.lhs0000644000000000000000000000011412761037130016060 0ustar0000000000000000#!/usr/bin/env runhaskell > import Distribution.Simple > main = defaultMain mersenne-random-pure64-0.2.2.0/cbits/0000755000000000000000000000000012761037130015360 5ustar0000000000000000mersenne-random-pure64-0.2.2.0/cbits/mt19937-64-block.c0000644000000000000000000000720012761037130020077 0ustar0000000000000000/* 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.2.0/cbits/mt19937-64.c0000644000000000000000000001007112761037130017007 0ustar0000000000000000/* 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.2.0/cbits/mt19937-64-unsafe.c0000644000000000000000000001025612761037130020273 0ustar0000000000000000/* 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.2.0/include/0000755000000000000000000000000012761037130015677 5ustar0000000000000000mersenne-random-pure64-0.2.2.0/include/mt19937-64-unsafe.h0000644000000000000000000000525712761037130020624 0ustar0000000000000000/* 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.2.0/include/mt19937-64.h0000644000000000000000000000574412761037130017346 0ustar0000000000000000/* 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.2.0/include/mt19937-64-block.h0000644000000000000000000000571012761037130020427 0ustar0000000000000000/* 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.2.0/System/0000755000000000000000000000000012761037130015540 5ustar0000000000000000mersenne-random-pure64-0.2.2.0/System/Random/0000755000000000000000000000000012761037130016760 5ustar0000000000000000mersenne-random-pure64-0.2.2.0/System/Random/Mersenne/0000755000000000000000000000000012761037130020534 5ustar0000000000000000mersenne-random-pure64-0.2.2.0/System/Random/Mersenne/Pure64.hs0000644000000000000000000001030712761037130022156 0ustar0000000000000000{-# LANGUAGE CPP, ForeignFunctionInterface #-} {-# OPTIONS_GHC -fno-warn-orphans #-} -------------------------------------------------------------------- -- | -- 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.Mersenne.Pure64.Internal import System.Random import Data.Word import Data.Int import Data.Time.Clock import Data.Time.Calendar import System.CPUTime -- | Create a PureMT generator from a 'Word64' seed. pureMT :: Word64 -> PureMT pureMT = mkPureMT . seedBlock . fromIntegral #if !MIN_VERSION_time(1,6,0) diffTimeToPicoseconds :: DiffTime -> Integer diffTimeToPicoseconds d = round (1000 * 1000 * 1000 * 1000 * d) #endif -- | Create a new PureMT generator, using the clocktime as the base for the seed. newPureMT :: IO PureMT newPureMT = do ct <- getCPUTime t <- getCurrentTime let seed = toModifiedJulianDay (utctDay t) + diffTimeToPicoseconds (utctDayTime t) + ct return $ pureMT $ fromIntegral seed ------------------------------------------------------------------------ -- 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 #-} -- create a new PureMT from an MTBlock mkPureMT :: MTBlock -> PureMT mkPureMT block = PureMT block 0 (nextBlock block) mersenne-random-pure64-0.2.2.0/System/Random/Mersenne/Pure64/0000755000000000000000000000000012761037130021621 5ustar0000000000000000mersenne-random-pure64-0.2.2.0/System/Random/Mersenne/Pure64/Base.hsc0000644000000000000000000000472312761037130023200 0ustar0000000000000000{-# 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.2.0/System/Random/Mersenne/Pure64/MTBlock.hs0000644000000000000000000000572412761037130023460 0ustar0000000000000000{-# 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 #if __GLASGOW_HASKELL__ >= 706 import GHC.IO #else import GHC.IOBase #endif import GHC.Word import System.Random.Mersenne.Pure64.Base import System.Random.Mersenne.Pure64.Internal 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.2.0/System/Random/Mersenne/Pure64/Internal.hs0000644000000000000000000000067012761037130023734 0ustar0000000000000000{-# LANGUAGE MagicHash #-} module System.Random.Mersenne.Pure64.Internal ( PureMT(..) , blockLen , blockSize , MTBlock(..) ) where import GHC.Exts import System.Random.Mersenne.Pure64.Base -- | 'PureMT', a pure mersenne twister pseudo-random number generator -- data PureMT = PureMT {-# UNPACK #-} !MTBlock {-# UNPACK #-} !Int MTBlock instance Show PureMT where show _ = show "" data MTBlock = MTBlock ByteArray#