NUMrandom.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /* NUMrandom.cpp
  2. *
  3. * Copyright (C) 1992-2011,2014,2015,2016,2017 Paul Boersma
  4. *
  5. * This code is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This code is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /*
  19. A C-program for MT19937-64 (2014/2/23 version).
  20. Coded by Takuji Nishimura and Makoto Matsumoto.
  21. This is a 64-bit version of Mersenne Twister pseudorandom number
  22. generator.
  23. Before using, initialize the state by using init_genrand64(seed)
  24. or init_by_array64(init_key, key_length).
  25. Copyright (C) 2004, 2014, Makoto Matsumoto and Takuji Nishimura,
  26. All rights reserved.
  27. Redistribution and use in source and binary forms, with or without
  28. modification, are permitted provided that the following conditions
  29. are met:
  30. 1. Redistributions of source code must retain the above copyright
  31. notice, this list of conditions and the following disclaimer.
  32. 2. Redistributions in binary form must reproduce the above copyright
  33. notice, this list of conditions and the following disclaimer in the
  34. documentation and/or other materials provided with the distribution.
  35. 3. The names of its contributors may not be used to endorse or promote
  36. products derived from this software without specific prior written
  37. permission.
  38. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  39. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  40. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  41. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  42. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  43. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  44. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  45. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  46. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  47. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  48. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  49. References:
  50. T. Nishimura, ``Tables of 64-bit Mersenne Twisters''
  51. ACM Transactions on Modeling and
  52. Computer Simulation 10. (2000) 348--357.
  53. M. Matsumoto and T. Nishimura,
  54. ``Mersenne Twister: a 623-dimensionally equidistributed
  55. uniform pseudorandom number generator''
  56. ACM Transactions on Modeling and
  57. Computer Simulation 8. (Jan. 1998) 3--30.
  58. Any feedback is very welcome.
  59. http://www.math.hiroshima-u.ac.jp/~m-mat/MT/emt.html
  60. email: m-mat @ math.sci.hiroshima-u.ac.jp (remove spaces)
  61. */
  62. #if defined (__MINGW32__) || defined (linux)
  63. #define UINT64_C(n) n ## ULL
  64. #endif
  65. #include <unistd.h>
  66. #include "melder.h"
  67. #define NN 312
  68. #define MM 156
  69. #define MATRIX_A UINT64_C (0xB5026F5AA96619E9)
  70. #define UM UINT64_C (0xFFFFFFFF80000000) /* Most significant 33 bits */
  71. #define LM UINT64_C (0x7FFFFFFF) /* Least significant 31 bits */
  72. class NUMrandom_State { public:
  73. /** The state vector.
  74. */
  75. uint64 array [NN];
  76. /** The pointer into the state vector.
  77. Equals NN + 1 iff the array has not been initialized.
  78. */
  79. int index;
  80. NUMrandom_State () : index (NN + 1) {}
  81. // this initialization will lead to an immediate crash
  82. // when NUMrandomFraction() is called without NUMrandom_init() having been called before;
  83. // without this initialization, it would be detected only after 312 calls to NUMrandomFraction()
  84. bool secondAvailable;
  85. double y;
  86. /**
  87. Initialize the whole array with one seed.
  88. This can be used for testing whether our implementation is correct (i.e. predicts the correct published sequence)
  89. and perhaps for generating reproducible sequences.
  90. */
  91. void init_genrand64 (uint64 seed) {
  92. array [0] = seed;
  93. for (index = 1; index < NN; index ++) {
  94. array [index] =
  95. (UINT64_C (6364136223846793005) * (array [index - 1] ^ (array [index - 1] >> 62))
  96. + (uint64) index);
  97. }
  98. }
  99. /* initialize by an array with array-length */
  100. /* init_key is the array for initializing keys */
  101. /* key_length is its length */
  102. void init_by_array64 (uint64 init_key[], unsigned int key_length);
  103. } states [17];
  104. /* initialize the array with a number of seeds */
  105. void NUMrandom_State :: init_by_array64 (uint64 init_key [], unsigned int key_length)
  106. {
  107. init_genrand64 (UINT64_C (19650218)); // warm it up
  108. unsigned int i = 1, j = 0;
  109. unsigned int k = ( NN > key_length ? NN : key_length );
  110. for (; k; k --) {
  111. array [i] = (array [i] ^ ((array [i - 1] ^ (array [i - 1] >> 62)) * UINT64_C (3935559000370003845)))
  112. + init_key [j] + (uint64) j; // non-linear
  113. i ++;
  114. j ++;
  115. if (i >= NN) { array [0] = array [NN - 1]; i = 1; }
  116. if (j >= key_length) j = 0;
  117. }
  118. for (k = NN - 1; k; k --) {
  119. array [i] = (array [i] ^ ((array [i - 1] ^ (array [i - 1] >> 62)) * UINT64_C (2862933555777941757)))
  120. - (uint64) i; // non-linear
  121. i ++;
  122. if (i >= NN) { array [0] = array [NN - 1]; i = 1; }
  123. }
  124. array [0] = UINT64_C (1) << 63; // MSB is 1; assuring non-zero initial array
  125. }
  126. static bool theInited = false;
  127. void NUMrandom_init () {
  128. for (int threadNumber = 0; threadNumber <= 16; threadNumber ++) {
  129. const int numberOfKeys = 6;
  130. uint64 keys [numberOfKeys];
  131. keys [0] = (uint64) llround (1e6 * Melder_clock ()); // unique between boots of the same computer
  132. keys [1] = UINT64_C (7320321686725470078) + (uint64) threadNumber; // unique between threads in the same process
  133. switch (threadNumber) {
  134. case 0: keys [2] = UINT64_C (4492812493098689432); keys [3] = UINT64_C (8902321878452586268); break;
  135. case 1: keys [2] = UINT64_C (1875086582568685862); keys [3] = UINT64_C (12243257483652989599); break;
  136. case 2: keys [2] = UINT64_C (9040925727554857487); keys [3] = UINT64_C (8037578605604605534); break;
  137. case 3: keys [2] = UINT64_C (11168476768576857685); keys [3] = UINT64_C (7862359785763816517); break;
  138. case 4: keys [2] = UINT64_C (3878901748368466876); keys [3] = UINT64_C (3563078257726526076); break;
  139. case 5: keys [2] = UINT64_C (2185735817578415800); keys [3] = UINT64_C (198502654671560756); break;
  140. case 6: keys [2] = UINT64_C (12248047509814562486); keys [3] = UINT64_C (9836250167165762757); break;
  141. case 7: keys [2] = UINT64_C (28362088588870143); keys [3] = UINT64_C (8756376201767075602); break;
  142. case 8: keys [2] = UINT64_C (5758130586486546775); keys [3] = UINT64_C (4213784157469743413); break;
  143. case 9: keys [2] = UINT64_C (8508416536565170756); keys [3] = UINT64_C (2856175717654375656); break;
  144. case 10: keys [2] = UINT64_C (2802356275260644756); keys [3] = UINT64_C (2309872134087235167); break;
  145. case 11: keys [2] = UINT64_C (230875784065064545); keys [3] = UINT64_C (1209802371478023476); break;
  146. case 12: keys [2] = UINT64_C (6520185868568714577); keys [3] = UINT64_C (2173615001556504015); break;
  147. case 13: keys [2] = UINT64_C (9082605608605765650); keys [3] = UINT64_C (1204167447560475647); break;
  148. case 14: keys [2] = UINT64_C (1238716515545475765); keys [3] = UINT64_C (8435674023875847388); break;
  149. case 15: keys [2] = UINT64_C (6127715675014756456); keys [3] = UINT64_C (2435788450287508457); break;
  150. case 16: keys [2] = UINT64_C (1081237546238975884); keys [3] = UINT64_C (2939783238574293882); break;
  151. default: Melder_fatal (U"Thread number too high.");
  152. }
  153. keys [4] = (uint64) (int64) getpid (); // unique between processes that run simultaneously on the same computer
  154. #ifndef _WIN32
  155. //keys [5] = (uint64) (int64) gethostid (); // unique between computers; but can be SLOW because it could have to access the internet
  156. #endif
  157. states [threadNumber]. init_by_array64 (keys, numberOfKeys);
  158. }
  159. theInited = true;
  160. }
  161. /* Throughout the years, several versions for "zero or magic" have been proposed. Choose the fastest. */
  162. #define ZERO_OR_MAGIC_VERSION 3
  163. #if ZERO_OR_MAGIC_VERSION == 1 // M&N 1999
  164. #define ZERO_OR_MAGIC ( (x & UINT64_C (1)) ? MATRIX_A : UINT64_C (0) )
  165. #elif ZERO_OR_MAGIC_VERSION == 2
  166. #define ZERO_OR_MAGIC ( (x & UINT64_C (1)) * MATRIX_A )
  167. #else // M&N 2014
  168. constexpr uint64 mag01 [2] { UINT64_C (0), MATRIX_A };
  169. #define ZERO_OR_MAGIC mag01 [(int) (x & UINT64_C (1))]
  170. #endif
  171. double NUMrandomFraction () {
  172. NUMrandom_State *me = & states [0];
  173. uint64 x;
  174. if (my index >= NN) { // generate NN words at a time
  175. Melder_assert (theInited); // if NUMrandom_init() hasn't been called, we'll detect that here, probably in the first call
  176. int i;
  177. for (i = 0; i < NN - MM; i ++) {
  178. x = (my array [i] & UM) | (my array [i + 1] & LM);
  179. my array [i] = my array [i + MM] ^ (x >> 1) ^ ZERO_OR_MAGIC;
  180. }
  181. for (; i < NN - 1; i ++) {
  182. x = (my array [i] & UM) | (my array [i + 1] & LM);
  183. my array [i] = my array [i + (MM - NN)] ^ (x >> 1) ^ ZERO_OR_MAGIC;
  184. }
  185. x = (my array [NN - 1] & UM) | (my array [0] & LM);
  186. my array [NN - 1] = my array [MM - 1] ^ (x >> 1) ^ ZERO_OR_MAGIC;
  187. my index = 0;
  188. }
  189. x = my array [my index ++];
  190. x ^= (x >> 29) & UINT64_C (0x5555555555555555);
  191. x ^= (x << 17) & UINT64_C (0x71D67FFFEDA60000);
  192. x ^= (x << 37) & UINT64_C (0xFFF7EEE000000000);
  193. x ^= (x >> 43);
  194. return (x >> 11) * (1.0/9007199254740992.0);
  195. }
  196. double NUMrandomFraction_mt (int threadNumber) {
  197. NUMrandom_State *me = & states [threadNumber];
  198. uint64 x;
  199. if (my index >= NN) { // generate NN words at a time
  200. Melder_assert (theInited);
  201. int i;
  202. for (i = 0; i < NN - MM; i ++) {
  203. x = (my array [i] & UM) | (my array [i + 1] & LM);
  204. my array [i] = my array [i + MM] ^ (x >> 1) ^ ZERO_OR_MAGIC;
  205. }
  206. for (; i < NN - 1; i ++) {
  207. x = (my array [i] & UM) | (my array [i + 1] & LM);
  208. my array [i] = my array [i + (MM - NN)] ^ (x >> 1) ^ ZERO_OR_MAGIC;
  209. }
  210. x = (my array [NN - 1] & UM) | (my array [0] & LM);
  211. my array [NN - 1] = my array [MM - 1] ^ (x >> 1) ^ ZERO_OR_MAGIC;
  212. my index = 0;
  213. }
  214. x = my array [my index ++];
  215. x ^= (x >> 29) & UINT64_C (0x5555555555555555);
  216. x ^= (x << 17) & UINT64_C (0x71D67FFFEDA60000);
  217. x ^= (x << 37) & UINT64_C (0xFFF7EEE000000000);
  218. x ^= (x >> 43);
  219. return (x >> 11) * (1.0/9007199254740992.0);
  220. }
  221. double NUMrandomUniform (double lowest, double highest) {
  222. return lowest + (highest - lowest) * NUMrandomFraction ();
  223. }
  224. integer NUMrandomInteger (integer lowest, integer highest) {
  225. return lowest + (integer) ((highest - lowest + 1) * NUMrandomFraction ()); // round down by truncation, because positive
  226. }
  227. bool NUMrandomBernoulli (double probability) {
  228. return NUMrandomFraction() < probability;
  229. }
  230. double NUMrandomBernoulli_real (double probability) {
  231. return (double) (NUMrandomFraction() < probability);
  232. }
  233. #define repeat do
  234. #define until(cond) while (! (cond))
  235. double NUMrandomGauss (double mean, double standardDeviation) {
  236. NUMrandom_State *me = & states [0];
  237. /*
  238. Knuth, p. 122.
  239. */
  240. if (my secondAvailable) {
  241. my secondAvailable = false;
  242. return mean + standardDeviation * my y;
  243. } else {
  244. double s, x;
  245. repeat {
  246. x = 2.0 * NUMrandomFraction () - 1.0; // inside the square [-1; 1] x [-1; 1]
  247. my y = 2.0 * NUMrandomFraction () - 1.0;
  248. s = x * x + my y * my y;
  249. } until (s < 1.0); // inside the unit circle
  250. if (s == 0.0) {
  251. x = my y = 0.0;
  252. } else {
  253. double factor = sqrt (-2.0 * log (s) / s);
  254. x *= factor;
  255. my y *= factor;
  256. }
  257. my secondAvailable = true;
  258. return mean + standardDeviation * x;
  259. }
  260. }
  261. double NUMrandomGauss_mt (int threadNumber, double mean, double standardDeviation) {
  262. NUMrandom_State *me = & states [threadNumber];
  263. /*
  264. Knuth, p. 122.
  265. */
  266. if (my secondAvailable) {
  267. my secondAvailable = false;
  268. return mean + standardDeviation * my y;
  269. } else {
  270. double s, x;
  271. repeat {
  272. x = 2.0 * NUMrandomFraction_mt (threadNumber) - 1.0; // inside the square [-1; 1] x [-1; 1]
  273. my y = 2.0 * NUMrandomFraction_mt (threadNumber) - 1.0;
  274. s = x * x + my y * my y;
  275. } until (s < 1.0); // inside the unit circle
  276. if (s == 0.0) {
  277. x = my y = 0.0;
  278. } else {
  279. double factor = sqrt (-2.0 * log (s) / s);
  280. x *= factor;
  281. my y *= factor;
  282. }
  283. my secondAvailable = true;
  284. return mean + standardDeviation * x;
  285. }
  286. }
  287. double NUMrandomPoisson (double mean) {
  288. /*
  289. The Poisson distribution is
  290. P(k) = mean^k * exp (- mean) / k!
  291. We have to find a function, with known primitive,
  292. that is always (a bit) greater than P (k).
  293. This function is based on the Lorentzian distribution,
  294. with a maximum of P(mean)/0.9 at k=mean:
  295. f (k) = mean^mean * exp (- mean) / mean! / (0.9 * (1 + (k - mean)^2 / (2 * mean)))
  296. The tangent is computed as the deviate
  297. tangent = tan (pi * unif (0, 1))
  298. This must equal the square root of (k - mean)^2 / (2 * mean),
  299. so that a trial value for k is given by
  300. k = floor (mean + tangent * sqrt (2 * mean))
  301. The probability that this is a good value is proportionate to the ratio of the Poisson
  302. distribution and the encapsulating function:
  303. probability = P (k) / f (k) = 0.9 * (1 + tangent^2) * mean ^ (k - mean) * mean! / k!
  304. The last two factors can be calculated as
  305. exp ((k - mean) * ln (mean) + lnGamma (mean + 1) - lnGamma (k + 1))
  306. */
  307. static double previousMean = -1.0; // this routine may well be called repeatedly with the same mean; optimize
  308. if (mean < 8.0) {
  309. static double expMean;
  310. double product = 1.0;
  311. integer result = -1;
  312. if (mean != previousMean) {
  313. previousMean = mean;
  314. expMean = exp (- mean);
  315. }
  316. repeat {
  317. product *= NUMrandomFraction ();
  318. result ++;
  319. } until (product <= expMean);
  320. return result;
  321. } else {
  322. static double sqrtTwoMean, lnMean, lnMeanFactorial;
  323. double result, probability, tangent;
  324. if (mean != previousMean) {
  325. previousMean = mean;
  326. sqrtTwoMean = sqrt (2.0 * mean);
  327. lnMean = log (mean);
  328. lnMeanFactorial = NUMlnGamma (mean + 1.0);
  329. }
  330. repeat {
  331. repeat {
  332. tangent = tan (NUMpi * NUMrandomFraction ());
  333. result = mean + tangent * sqrtTwoMean;
  334. } until (result >= 0.0);
  335. result = floor (result);
  336. probability = 0.9 * (1.0 + tangent * tangent) * exp ((result - mean) * lnMean + lnMeanFactorial - NUMlnGamma (result + 1.0));
  337. } until (NUMrandomFraction () <= probability);
  338. return result;
  339. }
  340. }
  341. uint32 NUMhashString (conststring32 string) {
  342. /*
  343. * Jenkins' one-at-a-time hash.
  344. */
  345. uint32 hash = 0;
  346. for (char32 kar = *string; kar != U'\0'; kar = * (++ string)) {
  347. hash += (kar >> 16) & 0xFF; // highest five bits (a char32 actually has only 21 significant bits)
  348. hash += (hash << 10);
  349. hash ^= (hash >> 6);
  350. hash += (kar >> 8) & 0xFF; // middle 8 bits
  351. hash += (hash << 10);
  352. hash ^= (hash >> 6);
  353. hash += kar & 0xFF; // lowest 8 bits
  354. hash += (hash << 10);
  355. hash ^= (hash >> 6);
  356. }
  357. hash += (hash << 3);
  358. hash ^= (hash >> 11);
  359. hash += (hash << 15);
  360. return hash;
  361. }
  362. /* End of file NUMrandom.cpp */