~ubuntu-branches/ubuntu/trusty/haskell-mersenne-random-pure64/trusty

« back to all changes in this revision

Viewing changes to cbits/mt19937-64-unsafe.c

  • Committer: Package Import Robot
  • Author(s): Joachim Breitner
  • Date: 2012-12-09 13:43:33 UTC
  • Revision ID: package-import@ubuntu.com-20121209134333-d6wrwl64m871qfgf
Tags: upstream-0.2.0.3
ImportĀ upstreamĀ versionĀ 0.2.0.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
   A C-program for MT19937-64 (2004/9/29 version).
 
3
   Coded by Takuji Nishimura and Makoto Matsumoto.
 
4
 
 
5
   This is a 64-bit version of Mersenne Twister pseudorandom number
 
6
   generator.
 
7
 
 
8
   Before using, initialize the state by using init_genrand64(seed)  
 
9
   or init_by_array64(init_key, key_length).
 
10
 
 
11
   Copyright (C) 2004, Makoto Matsumoto and Takuji Nishimura,
 
12
   All rights reserved.                          
 
13
 
 
14
   Redistribution and use in source and binary forms, with or without
 
15
   modification, are permitted provided that the following conditions
 
16
   are met:
 
17
 
 
18
     1. Redistributions of source code must retain the above copyright
 
19
        notice, this list of conditions and the following disclaimer.
 
20
 
 
21
     2. Redistributions in binary form must reproduce the above copyright
 
22
        notice, this list of conditions and the following disclaimer in the
 
23
        documentation and/or other materials provided with the distribution.
 
24
 
 
25
     3. The names of its contributors may not be used to endorse or promote 
 
26
        products derived from this software without specific prior written 
 
27
        permission.
 
28
 
 
29
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
30
   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
31
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
32
   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 
33
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
34
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
35
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 
36
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 
37
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 
38
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
39
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
40
 
 
41
   References:
 
42
   T. Nishimura, ``Tables of 64-bit Mersenne Twisters''
 
43
     ACM Transactions on Modeling and 
 
44
     Computer Simulation 10. (2000) 348--357.
 
45
   M. Matsumoto and T. Nishimura,
 
46
     ``Mersenne Twister: a 623-dimensionally equidistributed
 
47
       uniform pseudorandom number generator''
 
48
     ACM Transactions on Modeling and 
 
49
     Computer Simulation 8. (Jan. 1998) 3--30.
 
50
 
 
51
   Any feedback is very welcome.
 
52
   http://www.math.hiroshima-u.ac.jp/~m-mat/MT/emt.html
 
53
   email: m-mat @ math.sci.hiroshima-u.ac.jp (remove spaces)
 
54
*/
 
55
 
 
56
 
 
57
#include <stdio.h>
 
58
#include "mt19937-64-unsafe.h"
 
59
 
 
60
#define NN 312
 
61
#define MM 156
 
62
#define MATRIX_A 0xB5026F5AA96619E9ULL
 
63
#define UM 0xFFFFFFFF80000000ULL /* Most significant 33 bits */
 
64
#define LM 0x7FFFFFFFULL /* Least significant 31 bits */
 
65
 
 
66
 
 
67
/* The array for the state vector */
 
68
static unsigned long long mt[NN]; 
 
69
/* mti==NN+1 means mt[NN] is not initialized */
 
70
static int mti=NN+1; 
 
71
 
 
72
/* initializes mt[NN] with a seed */
 
73
void init_genrand64_unsafe(unsigned long long seed)
 
74
{
 
75
    mt[0] = seed;
 
76
    for (mti=1; mti<NN; mti++) 
 
77
        mt[mti] =  (6364136223846793005ULL * (mt[mti-1] ^ (mt[mti-1] >> 62)) + mti);
 
78
}
 
79
 
 
80
/* generates a random number on [0, 2^64-1]-interval */
 
81
unsigned long long genrand64_int64_unsafe(void)
 
82
{
 
83
    int i;
 
84
    unsigned long long x;
 
85
    static unsigned long long mag01[2]={0ULL, MATRIX_A};
 
86
 
 
87
    if (mti >= NN) { /* generate NN words at one time */
 
88
 
 
89
        /* if init_genrand64() has not been called, */
 
90
        /* a default initial seed is used     */
 
91
        if (mti == NN+1) 
 
92
            init_genrand64_unsafe(5489ULL); 
 
93
 
 
94
        for (i=0;i<NN-MM;i++) {
 
95
            x = (mt[i]&UM)|(mt[i+1]&LM);
 
96
            mt[i] = mt[i+MM] ^ (x>>1) ^ mag01[(int)(x&1ULL)];
 
97
        }
 
98
        for (;i<NN-1;i++) {
 
99
            x = (mt[i]&UM)|(mt[i+1]&LM);
 
100
            mt[i] = mt[i+(MM-NN)] ^ (x>>1) ^ mag01[(int)(x&1ULL)];
 
101
        }
 
102
        x = (mt[NN-1]&UM)|(mt[0]&LM);
 
103
        mt[NN-1] = mt[MM-1] ^ (x>>1) ^ mag01[(int)(x&1ULL)];
 
104
 
 
105
        mti = 0;
 
106
    }
 
107
  
 
108
    x = mt[mti++];
 
109
 
 
110
    x ^= (x >> 29) & 0x5555555555555555ULL;
 
111
    x ^= (x << 17) & 0x71D67FFFEDA60000ULL;
 
112
    x ^= (x << 37) & 0xFFF7EEE000000000ULL;
 
113
    x ^= (x >> 43);
 
114
 
 
115
    return x;
 
116
}
 
117
 
 
118
/* generates a random number on [0,1)-real-interval */
 
119
double genrand64_real2_unsafe(void)
 
120
{
 
121
    return (genrand64_int64_unsafe() >> 11) * (1.0/9007199254740992.0);
 
122
}