Download code
From LiteratePrograms
Back to Mersenne_twister_(C)
Download for Windows: single file, zip
Download for UNIX: single file, zip, tar.gz, tar.bz2
mt.c
1 /* Copyright (c) 2010 the authors listed at the following URL, and/or 2 the authors of referenced articles or incorporated external code: 3 http://en.literateprograms.org/Mersenne_twister_(C)?action=history&offset=20081027093704 4 5 Permission is hereby granted, free of charge, to any person obtaining 6 a copy of this software and associated documentation files (the 7 "Software"), to deal in the Software without restriction, including 8 without limitation the rights to use, copy, modify, merge, publish, 9 distribute, sublicense, and/or sell copies of the Software, and to 10 permit persons to whom the Software is furnished to do so, subject to 11 the following conditions: 12 13 The above copyright notice and this permission notice shall be 14 included in all copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 24 Retrieved from: http://en.literateprograms.org/Mersenne_twister_(C)?oldid=15318 25 */ 26 27 28 /* This program implements the Mersenne twister algorithm for generation of pseudorandom numbers. 29 The program returns random integers in the range 0 to 2^32-1 (this holds even if a long int is 30 larger than 32 bits). Timing with gcc indicates that it is about twice as fast as the built in 31 rand function. The original code was written by Michael Brundage and has been placed in the 32 public domain. There are a three minor changes here: 33 (1) This comment has been added to the program. 34 (2) Type specifiers (ul) have been appended to constants. 35 (3) A commented out block near the end has been removed. */ 36 37 #define MT_LEN 624 38 #include <stdlib.h> 39 40 int mt_index; 41 unsigned long mt_buffer[MT_LEN]; 42 43 void mt_init() { 44 int i; 45 for (i = 0; i < MT_LEN; i++) 46 mt_buffer[i] = rand(); 47 mt_index = 0; 48 } 49 50 #define MT_IA 397 51 #define MT_IB (MT_LEN - MT_IA) 52 #define UPPER_MASK 0x80000000 53 #define LOWER_MASK 0x7FFFFFFF 54 #define MATRIX_A 0x9908B0DF 55 #define TWIST(b,i,j) ((b)[i] & UPPER_MASK) | ((b)[j] & LOWER_MASK) 56 #define MAGIC(s) (((s)&1)*MATRIX_A) 57 58 unsigned long mt_random() { 59 unsigned long * b = mt_buffer; 60 int idx = mt_index; 61 unsigned long s; 62 int i; 63 64 if (idx == MT_LEN*sizeof(unsigned long)) 65 { 66 idx = 0; 67 i = 0; 68 for (; i < MT_IB; i++) { 69 s = TWIST(b, i, i+1); 70 b[i] = b[i + MT_IA] ^ (s >> 1) ^ MAGIC(s); 71 } 72 for (; i < MT_LEN-1; i++) { 73 s = TWIST(b, i, i+1); 74 b[i] = b[i - MT_IB] ^ (s >> 1) ^ MAGIC(s); 75 } 76 77 s = TWIST(b, MT_LEN-1, 0); 78 b[MT_LEN-1] = b[MT_IA-1] ^ (s >> 1) ^ MAGIC(s); 79 } 80 mt_index = idx + sizeof(unsigned long); 81 return *(unsigned long *)((unsigned char *)b + idx); 82 /* Here there is a commented out block in MB's original program */ 83 } 84
