NAPA Compiler V4.50
Author: Yves Leduc, yves.leduc.be@gmail.com
Loading...
Searching...
No Matches
C:/Simulate/Napados/Source/rg.c
Go to the documentation of this file.
1/* ** INTERNAL PSEUDO RANDOM NUMBER GENERATOR *********************************************************************************** */
2
3#undef EXTERN
4#define EXTERN extern
5
6#include "./napa.h"
7#include "./proto.h"
8
9
10/* *** In this file: ************************************************************************************************************ */
11
12/* void random_functions(void) */
13/* void randomizer_functions(void) */
14
15
16/* ***************************************************************************************************************************** */
17
18/* **** PLEASE NOTE **** */
19/* */
20/* Source http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/VERSIONS/C-LANG/mt19937-64.c */
21/* */
22/* THIS IS THE LEGAL TEXT FROM THE AUTHORS. The original work has been slightly edited by Yves Leduc to fit the simulator NAPA. */
23
24/* A C-program for MT19937-64 (2004/9/29 version) coded by Takuji Nishimura and Makoto Matsumoto. */
25/* This is a 64-bit version of Mersenne Twister pseudorandom number generator. */
26/* Copyright (C) 2004, Makoto Matsumoto and Takuji Nishimura, All rights reserved. */
27/* */
28/* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following */
29/* conditions are met: */
30/* */
31/* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following */
32/* disclaimer. */
33/* */
34/* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following */
35/* disclaimer in the documentation and/or other materials provided with the distribution. */
36/* */
37/* 3. The names of its contributors may not be used to endorse or promote products derived from this software without */
38/* specific prior written permission. */
39/* */
40/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, */
41/* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
42/* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, */
43/* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF */
44/* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT */
45/* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED */
46/* OF THE POSSIBILITY OF SUCH DAMAGE. */
47/* */
48/* References: */
49/* */
50/* T. Nishimura, ``Tables of 64-bit Mersenne Twisters'' */
51/* ACM Transactions on Modeling and Computer Simulation 10. (2000) 348--357. */
52/* */
53/* M. Matsumoto and T. Nishimura, */
54/* ``Mersenne Twister: a 623-dimensionally equidistributed uniform pseudorandom number generator'' */
55/* ACM Transactions on Modeling and Computer Simulation 8. (Jan. 1998) 3--30. */
56/* */
57/* Any feedback is very welcome. */
58/* http://www.math.hiroshima-u.ac.jp/~m-mat/MT/emt.html */
59/* email: m-mat @ math.sci.hiroshima-u.ac.jp (remove spaces) */
60
61
62/* ****************************************************************************************************************************** */
63
64/* Here is the mechanism used by the pseudo random noise generators of NAPA: */
65
66/* A seed is obtained by 'napa_seed()' from the operating system using the ANSI-C instruction 'time()' or from a seed provided by */
67/* user (NAPA instruction 'random_seed'). */
68
69/* A seed randomizer 'napa_root' is used to initialize the internal registers of the pseudo random noise generator 'napa_rand'. */
70
71/* All functions (the seed generator, the seed randomizer and the noise generator itself) */
72/* are included in the NAPA simulator (the C file generated by the NAPA compiler). */
73
74/* Mechanism is built to replace them by external user-defined functions. */
75
76/* During the simulation, all the process is deterministic and may be replayed, if the initial seed is reused. This initial seed */
77/* is documented in the C file generated by the NAPA compiler. It is recommended that it appears in all ouptut files produced by */
78/* the simulation (this responsibility is left to the writer of the user defined tools). */
79
80
81/* ****************************************************************************************************************************** */
82
83/* An external simulator could be placed in a header file containing the code: */
84
85/* Redirections to inactivate internal generators and activate the new generators: */
86
87/* #define EXTERNAL_PSEUDO_RANDOM_NUMBER_GENERATOR */
88/* #undef NAPA_SEED */
89/* #define NAPA_SEED &my_external_seed_generator */
90/* #undef NAPA_RAND */
91/* #define NAPA_RAND &my_external_rand_generator */
92
93/* Prototypes */
94
95/* void my_external_seed_generator(I_TYPE); */
96/* I_TYPE my_external_rand_generator(void); */
97
98/* Functions */
99
100/* void my_external_seed_generator(I_TYPE seed) { */
101/* .. (have a look on the internal generators as examples) */
102/* return; */
103/* } */
104
105/* I_TYPE my_external_rand_generator(void); */
106/* I_TYPE s; */
107/* .. (have a look on the internal generators as examples) */
108/* return s; */
109/* } */
110
111
112/* To activate the external random number generator and desactivate the internal one, place in the NAPA main file: */
113
114/* header "my_file.hdr" // file containing the code as described above, */
115/* or use a specific directive to trigger the inclusion of this header. */
116
117
118/* ****************************************************************************************************************************** */
119
121 (void) fprintf(STDOUT, "\n\n/* *** SIMPLE EQUILIKELY DISTRIBUTED POSITIVE INTEGER NUMBER ");
122 (void) fprintf(STDOUT, "PSEUDO-RANDOM GENERATOR (MATSUMOTO & NISHIMURA IMPROVED LCG) **** */\n\n");
123 (void) fprintf(STDOUT, "I_TYPE napa_rand_integer(I_TYPE range) { %*s /* Output in [0, range] */\n", 57, " ");
124 (void) fprintf(STDOUT, " unsigned long ulrnd;\n");
125 (void) fprintf(STDOUT, " static unsigned long j = 1UL;\n");
126 (void) fprintf(STDOUT, " ulrnd = ((unsigned long) rand()) & 0XFFFFFFFFUL;\n");
127 (void) fprintf(STDOUT, " ulrnd = j + (1812433253UL * (ulrnd^(ulrnd >> 30)));\n");
128 (void) fprintf(STDOUT, " j++;\n");
129 (void) fprintf(STDOUT, " return ((I_TYPE) (ulrnd & 0XFFFFFFFFUL)) %% (1LL + ABS(range));\n");
130 (void) fprintf(STDOUT, "}\n");
131 (void) fprintf(STDOUT, "\n\n/* *** SEED RANDOMIZER (MATSUMOTO & NISHIMURA IMPROVED LCG) %s */\n\n", multiple('*', 66L));
132 (void) fprintf(STDOUT, "#if !defined(EXTERNAL_PSEUDO_RANDOM_NUMBER_GENERATOR)\n\n");
133 (void) fprintf(STDOUT, "I_TYPE napa_default_root(I_TYPE seed) {\n");
134 (void) fprintf(STDOUT, " unsigned long ulrnd;\n");
135 (void) fprintf(STDOUT, " static unsigned long j = 1UL;\n");
136 (void) fprintf(STDOUT, " ulrnd = ((unsigned long) seed) & 0XFFFFFFFFUL;\n");
137 (void) fprintf(STDOUT, " ulrnd = j + (1812433253UL * (ulrnd^(ulrnd >> 30)));\n");
138 (void) fprintf(STDOUT, " j++;\n");
139 (void) fprintf(STDOUT, " return (I_TYPE) (ulrnd & 0XFFFFFFFFUL);\n");
140 (void) fprintf(STDOUT, "}\n\n");
141 (void) fprintf(STDOUT, "#endif\n\n");
142 return;
143}
144
145
146/* ****************************************************************************************************************************** */
147
149 (void) fprintf(STDOUT, "\n/* *** PSEUDO-RANDOM NUMBER GENERATOR (MERSENNE TWISTER MT19937-64) %s */\n", multiple('*', 59L));
150 (void) fprintf(STDOUT, "/* *** Its period is 2^19937-1, which is approximately equal to 10^6002 %50s **** */\n", " ");
151 (void) fprintf(STDOUT, "/* *** Source: Takuji Nishimura and Makoto Matsumoto, version 2004/9/29 %50s **** */\n", " ");
152 (void) fprintf(STDOUT, "/* *** Sligthly modified to fit NAPA %6s %72s **** */\n\n", NAPA_COMPILER_VERSION, " ");
153 (void) fprintf(STDOUT, "#if !defined(EXTERNAL_PSEUDO_RANDOM_NUMBER_GENERATOR)\n\n");
154
155 (void) fprintf(STDOUT, "unsigned long long napa_random_mta[312];\n");
156 (void) fprintf(STDOUT, "int napa_random_mti = 0;\n\n");
157
158 (void) fprintf(STDOUT, "I_TYPE napa_default_seed(I_TYPE seed) {\n");
159 (void) fprintf(STDOUT, " time_t tsys;\n");
160 (void) fprintf(STDOUT, " if ( 0LL == seed ) {\n");
161 (void) fprintf(STDOUT, " tsys = time((time_t*) NULL);\n");
162 (void) fprintf(STDOUT, " seed = ((I_TYPE) tsys) / 2LL;\n");
163 (void) fprintf(STDOUT, " } else {\n");
164 (void) fprintf(STDOUT, " seed = ABS(seed);\n");
165 (void) fprintf(STDOUT, " }\n");
166 (void) fprintf(STDOUT, " napa_random_mta[0] = (unsigned long long) napa_root(seed);\n");
167 (void) fprintf(STDOUT, " for (napa_random_mti = 1; napa_random_mti < 312; napa_random_mti++) {\n");
168 (void) fprintf(STDOUT, " napa_random_mta[napa_random_mti] = napa_random_mta[napa_random_mti-1];\n");
169 (void) fprintf(STDOUT, " napa_random_mta[napa_random_mti] ^= napa_random_mta[napa_random_mti-1] >> 62;\n");
170 (void) fprintf(STDOUT, " napa_random_mta[napa_random_mti] *= 6364136223846793005ULL;\n");
171 (void) fprintf(STDOUT, " napa_random_mta[napa_random_mti] += napa_random_mti;\n");
172 (void) fprintf(STDOUT, " }\n");
173 (void) fprintf(STDOUT, " return seed;\n");
174 (void) fprintf(STDOUT, "}\n\n");
175
176 (void) fprintf(STDOUT, "I_TYPE napa_default_rand(void) {\n"); /* generates on [0, 2^63-1]-interval */
177 (void) fprintf(STDOUT, " int i;\n");
178 (void) fprintf(STDOUT, " unsigned long long x;\n");
179 (void) fprintf(STDOUT, " static const unsigned long long mag01[2] = {0ULL, 0XB5026F5AA96619E9ULL};\n");
180 (void) fprintf(STDOUT, " if ( 312 <= napa_random_mti ) {\n");
181 (void) fprintf(STDOUT, " for (i = 0; i < 156; i++) {\n");
182 (void) fprintf(STDOUT, " x = (napa_random_mta[ i] & 0XFFFFFFFF80000000ULL) | (napa_random_mta[i+1] & 0X7FFFFFFFULL);\n");
183 (void) fprintf(STDOUT, " napa_random_mta[i] = napa_random_mta[i+156] ^ (x>>1) ^ mag01[(int) (x&1ULL)];\n");
184 (void) fprintf(STDOUT, " }\n");
185 (void) fprintf(STDOUT, " for (i = 156; i < 311; i++) {\n");
186 (void) fprintf(STDOUT, " x = (napa_random_mta[ i] & 0XFFFFFFFF80000000ULL) | (napa_random_mta[i+1] & 0X7FFFFFFFULL);\n");
187 (void) fprintf(STDOUT, " napa_random_mta[i] = napa_random_mta[i-156] ^ (x>>1) ^ mag01[(int) (x&1ULL)];\n");
188 (void) fprintf(STDOUT, " }\n");
189 (void) fprintf(STDOUT, " x = (napa_random_mta[311] & 0XFFFFFFFF80000000ULL) | (napa_random_mta[ 0] & 0X7FFFFFFFULL);\n");
190 (void) fprintf(STDOUT, " napa_random_mta[311] = napa_random_mta[ 155] ^ (x>>1) ^ mag01[(int) (x&1ULL)];\n");
191 (void) fprintf(STDOUT, " napa_random_mti = 0;\n");
192 (void) fprintf(STDOUT, " }\n");
193 (void) fprintf(STDOUT, " x = napa_random_mta[napa_random_mti++];\n");
194 (void) fprintf(STDOUT, " x ^= (x >> 29) & 0X5555555555555555ULL;\n");
195 (void) fprintf(STDOUT, " x ^= (x << 17) & 0X71D67FFFEDA60000ULL;\n");
196 (void) fprintf(STDOUT, " x ^= (x << 37) & 0XFFF7EEE000000000ULL;\n");
197 (void) fprintf(STDOUT, " x ^= (x >> 43);\n");
198 (void) fprintf(STDOUT, " return (I_TYPE) (x >> 1);\n"); /* unsigned long long to positive signed long long */
199 (void) fprintf(STDOUT, "}\n\n");
200 (void) fprintf(STDOUT, "#endif\n");
201 return;
202}
203
204
205/* ****************************************************************************************************************************** */
206/* end of file */
#define NAPA_COMPILER_VERSION
Definition napa.h:30
#define STDOUT
Definition napa.h:104
char * multiple(char c, long n)
Definition tk.c:1801
void random_functions(void)
Definition rg.c:148
void randomizer_functions(void)
Definition rg.c:120