Download code

Jump to: navigation, search

Back to Mersenne_twister_(C)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

mt.c

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Mersenne_twister_(C)?oldid=15318
14 */
15 
16 
17 /* This program implements the Mersenne twister algorithm for generation of pseudorandom numbers. 
18 The program returns random integers in the range 0 to 2^32-1 (this holds even if a long int is
19 larger than 32 bits). Timing with gcc indicates that it is about twice as fast as the built in 
20 rand function. The original code was written by Michael Brundage and has been placed in the 
21 public domain. There are a three minor changes here: 
22 (1) This comment has been added to the program.
23 (2) Type specifiers (ul) have been appended to constants.
24 (3) A commented out block near the end has been removed. */
25 
26 #define MT_LEN 624
27 #include <stdlib.h>
28 
29 int mt_index;
30 unsigned long mt_buffer[MT_LEN];
31 
32 void mt_init() {
33     int i;
34     for (i = 0; i < MT_LEN; i++)
35         mt_buffer[i] = rand();
36     mt_index = 0;
37 }
38 
39 #define MT_IA           397
40 #define MT_IB           (MT_LEN - MT_IA)
41 #define UPPER_MASK      0x80000000
42 #define LOWER_MASK      0x7FFFFFFF
43 #define MATRIX_A        0x9908B0DF
44 #define TWIST(b,i,j)    ((b)[i] & UPPER_MASK) | ((b)[j] & LOWER_MASK)
45 #define MAGIC(s)        (((s)&1)*MATRIX_A)
46 
47 unsigned long mt_random() {
48     unsigned long * b = mt_buffer;
49     int idx = mt_index;
50     unsigned long s;
51     int i;
52 	
53     if (idx == MT_LEN*sizeof(unsigned long))
54     {
55         idx = 0;
56         i = 0;
57         for (; i < MT_IB; i++) {
58             s = TWIST(b, i, i+1);
59             b[i] = b[i + MT_IA] ^ (s >> 1) ^ MAGIC(s);
60         }
61         for (; i < MT_LEN-1; i++) {
62             s = TWIST(b, i, i+1);
63             b[i] = b[i - MT_IB] ^ (s >> 1) ^ MAGIC(s);
64         }
65         
66         s = TWIST(b, MT_LEN-1, 0);
67         b[MT_LEN-1] = b[MT_IA-1] ^ (s >> 1) ^ MAGIC(s);
68     }
69     mt_index = idx + sizeof(unsigned long);
70     return *(unsigned long *)((unsigned char *)b + idx);
71     /* Here there is a commented out block in MB's original program */
72 }
73 


hijacker
hijacker
hijacker
hijacker