GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
randmtzig.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2006-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 /*
27  A C-program for MT19937, with initialization improved 2002/2/10.
28  Coded by Takuji Nishimura and Makoto Matsumoto.
29  This is a faster version by taking Shawn Cokus's optimization,
30  Matthe Bellew's simplification, Isaku Wada's real version.
31  David Bateman added normal and exponential distributions following
32  Marsaglia and Tang's Ziggurat algorithm.
33 
34  Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
35  Copyright (C) 2004, David Bateman
36  All rights reserved.
37 
38  Redistribution and use in source and binary forms, with or without
39  modification, are permitted provided that the following conditions
40  are met:
41 
42  1. Redistributions of source code must retain the above copyright
43  notice, this list of conditions and the following disclaimer.
44 
45  2. Redistributions in binary form must reproduce the above copyright
46  notice, this list of conditions and the following disclaimer in the
47  documentation and/or other materials provided with the distribution.
48 
49  3. The names of its contributors may not be used to endorse or promote
50  products derived from this software without specific prior written
51  permission.
52 
53  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
54  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
55  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
56  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
57  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
58  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
59  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
60  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
61  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
62  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
63  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64 
65 
66  Any feedback is very welcome.
67  http://www.math.keio.ac.jp/matumoto/emt.html
68  email: matumoto@math.keio.ac.jp
69 
70  * 2004-01-19 Paul Kienzle
71  * * comment out main
72  * add init_by_entropy, get_state, set_state
73  * * converted to allow compiling by C++ compiler
74  *
75  * 2004-01-25 David Bateman
76  * * Add Marsaglia and Tsang Ziggurat code
77  *
78  * 2004-07-13 Paul Kienzle
79  * * make into an independent library with some docs.
80  * * introduce new main and test code.
81  *
82  * 2004-07-28 Paul Kienzle & David Bateman
83  * * add -DALLBITS flag for 32 vs. 53 bits of randomness in mantissa
84  * * make the naming scheme more uniform
85  * * add -DHAVE_X86 for faster support of 53 bit mantissa on x86 arch.
86  *
87  * 2005-02-23 Paul Kienzle
88  * * fix -DHAVE_X86_32 flag and add -DUSE_X86_32=0|1 for explicit control
89  *
90  * 2006-04-01 David Bateman
91  * * convert for use in octave, declaring static functions only used
92  * here and adding oct_ to functions visible externally
93  * * inverse sense of ALLBITS
94  *
95  * 2012-05-18 David Bateman
96  * * Remove randu64 and ALLBIT option
97  * * Add the single precision generators
98  */
99 
100 /*
101  === Build instructions ===
102 
103  Compile with -DHAVE_GETTIMEOFDAY if the gettimeofday function is
104  available. This is not necessary if your architecture has
105  /dev/urandom defined.
106 
107  Uses implicit -Di386 or explicit -DHAVE_X86_32 to determine if CPU=x86.
108  You can force X86 behavior with -DUSE_X86_32=1, or suppress it with
109  -DUSE_X86_32=0. You should also consider -march=i686 or similar for
110  extra performance. Check whether -DUSE_X86_32=0 is faster on 64-bit
111  x86 architectures.
112 
113  If you want to replace the Mersenne Twister with another
114  generator then redefine randi32 appropriately.
115 
116  === Usage instructions ===
117  Before using any of the generators, initialize the state with one of
118  the init_mersenne_twister functions.
119 
120  All generators share the same state vector.
121 
122  === Mersenne Twister ===
123  random initial state:
124  void init_mersenne_twister (void)
125 
126  // 32-bit initial state:
127  void init_mersenne_twister (uint32_t s)
128 
129  // m*32-bit initial state:
130  void init_mersenne_twister (uint32_t k[],int m)
131 
132  // saves state in array:
133  void get_mersenne_twister_state (uint32_t save[MT_N+1])
134 
135  // restores state from array
136  void set_mersenne_twister_state (uint32_t save[MT_N+1])
137 
138  static uint32_t randmt (void) returns 32-bit unsigned int
139 
140  === inline generators ===
141  static uint32_t randi32 (void) returns 32-bit unsigned int
142  static uint64_t randi53 (void) returns 53-bit unsigned int
143  static uint64_t randi54 (void) returns 54-bit unsigned int
144  static float randu24 (void) returns 24-bit uniform in (0,1)
145  static double randu53 (void) returns 53-bit uniform in (0,1)
146 
147  double rand_uniform (void) returns M-bit uniform in (0,1)
148  double rand_normal (void) returns M-bit standard normal
149  double rand_exponential (void) returns N-bit standard exponential
150 
151  === Array generators ===
152  void rand_uniform (octave_idx_type, double [])
153  void rand_normal (octave_idx_type, double [])
154  void rand_exponential (octave_idx_type, double [])
155 */
156 
157 #if defined (HAVE_CONFIG_H)
158 # include "config.h"
159 #endif
160 
161 #include <cmath>
162 #include <cstdio>
163 
164 #include <algorithm>
165 
166 #include "oct-time.h"
167 #include "randmtzig.h"
168 
169 /* FIXME: may want to suppress X86 if sizeof(long) > 4 */
170 #if ! defined (USE_X86_32)
171 # if defined (i386) || defined (HAVE_X86_32)
172 # define USE_X86_32 1
173 # else
174 # define USE_X86_32 0
175 # endif
176 #endif
177 
178 namespace octave
179 {
180  /* ===== Mersenne Twister 32-bit generator ===== */
181 
182 #define MT_M 397
183 #define MATRIX_A 0x9908b0dfUL /* constant vector a */
184 #define UMASK 0x80000000UL /* most significant w-r bits */
185 #define LMASK 0x7fffffffUL /* least significant r bits */
186 #define MIXBITS(u,v) ( ((u) & UMASK) | ((v) & LMASK) )
187 #define TWIST(u,v) ((MIXBITS(u,v) >> 1) ^ ((v)&1UL ? MATRIX_A : 0UL))
188 
189  static uint32_t *next;
190  static uint32_t state[MT_N]; /* the array for the state vector */
191  static int left = 1;
192  static int initf = 0;
193  static int initt = 1;
194  static int inittf = 1;
195 
196  /* initializes state[MT_N] with a seed */
197  void init_mersenne_twister (const uint32_t s)
198  {
199  int j;
200  state[0] = s & 0xffffffffUL;
201  for (j = 1; j < MT_N; j++)
202  {
203  state[j] = (1812433253UL * (state[j-1] ^ (state[j-1] >> 30)) + j);
204  /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
205  /* In the previous versions, MSBs of the seed affect */
206  /* only MSBs of the array state[]. */
207  /* 2002/01/09 modified by Makoto Matsumoto */
208  state[j] &= 0xffffffffUL; /* for >32 bit machines */
209  }
210  left = 1;
211  initf = 1;
212  }
213 
214  /* initialize by an array with array-length */
215  /* init_key is the array for initializing keys */
216  /* key_length is its length */
217  void init_mersenne_twister (const uint32_t *init_key, const int key_length)
218  {
219  int i, j, k;
220  init_mersenne_twister (19650218UL);
221  i = 1;
222  j = 0;
223  k = (MT_N > key_length ? MT_N : key_length);
224  for (; k; k--)
225  {
226  state[i] = (state[i] ^ ((state[i-1] ^ (state[i-1] >> 30)) * 1664525UL))
227  + init_key[j] + j; /* non linear */
228  state[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
229  i++;
230  j++;
231  if (i >= MT_N)
232  {
233  state[0] = state[MT_N-1];
234  i = 1;
235  }
236  if (j >= key_length)
237  j = 0;
238  }
239  for (k = MT_N - 1; k; k--)
240  {
241  state[i] = (state[i] ^ ((state[i-1] ^ (state[i-1] >> 30)) * 1566083941UL))
242  - i; /* non linear */
243  state[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
244  i++;
245  if (i >= MT_N)
246  {
247  state[0] = state[MT_N-1];
248  i = 1;
249  }
250  }
251 
252  state[0] = 0x80000000UL; /* MSB is 1; assuring nonzero initial array */
253  left = 1;
254  initf = 1;
255  }
256 
258  {
259  uint32_t entropy[MT_N];
260  int n = 0;
261 
262  /* Look for entropy in /dev/urandom */
263  FILE *urandom = std::fopen ("/dev/urandom", "rb");
264  if (urandom)
265  {
266  while (n < MT_N)
267  {
268  unsigned char word[4];
269  if (std::fread (word, 4, 1, urandom) != 1)
270  break;
271  entropy[n++] = word[0] + (word[1]<<8) + (word[2]<<16)
272  + (static_cast<uint32_t> (word[3])<<24);
273  }
274  std::fclose (urandom);
275  }
276 
277  /* If there isn't enough entropy, gather some from various sources */
278 
279  sys::time now;
280 
281  if (n < MT_N)
282  entropy[n++] = now.unix_time (); /* Current time in seconds */
283 
284  if (n < MT_N)
285  entropy[n++] = clock (); /* CPU time used (usec) */
286 
287  if (n < MT_N)
288  entropy[n++] = now.usec (); /* Fractional part of current time */
289 
290  /* Send all the entropy into the initial state vector */
291  init_mersenne_twister (entropy,n);
292  }
293 
294  void set_mersenne_twister_state (const uint32_t *save)
295  {
296  std::copy_n (save, MT_N, state);
297  left = save[MT_N];
298  next = state + (MT_N - left + 1);
299  }
300 
301  void get_mersenne_twister_state (uint32_t *save)
302  {
303  std::copy_n (state, MT_N, save);
304  save[MT_N] = left;
305  }
306 
307  static void next_state (void)
308  {
309  uint32_t *p = state;
310  int j;
311 
312  /* if init_by_int() has not been called, */
313  /* a default initial seed is used */
314  /* if (initf==0) init_by_int(5489UL); */
315  /* Or better yet, a random seed! */
316  if (initf == 0)
318 
319  left = MT_N;
320  next = state;
321 
322  for (j = MT_N - MT_M + 1; --j; p++)
323  *p = p[MT_M] ^ TWIST(p[0], p[1]);
324 
325  for (j = MT_M; --j; p++)
326  *p = p[MT_M-MT_N] ^ TWIST(p[0], p[1]);
327 
328  *p = p[MT_M-MT_N] ^ TWIST(p[0], state[0]);
329  }
330 
331  /* generates a random number on [0,0xffffffff]-interval */
332  static uint32_t randmt (void)
333  {
334  uint32_t y;
335 
336  if (--left == 0)
337  next_state ();
338  y = *next++;
339 
340  /* Tempering */
341  y ^= (y >> 11);
342  y ^= (y << 7) & 0x9d2c5680UL;
343  y ^= (y << 15) & 0xefc60000UL;
344  return (y ^ (y >> 18));
345  }
346 
347  /* ===== Uniform generators ===== */
348 
349  /* Select which 32 bit generator to use */
350 #define randi32 randmt
351 
352  static uint64_t randi53 (void)
353  {
354  const uint32_t lo = randi32 ();
355  const uint32_t hi = randi32 () & 0x1FFFFF;
356 #if defined (HAVE_X86_32)
357  uint64_t u;
358  uint32_t *p = (uint32_t *)&u;
359  p[0] = lo;
360  p[1] = hi;
361  return u;
362 #else
363  return ((static_cast<uint64_t> (hi) << 32) | lo);
364 #endif
365  }
366 
367  static uint64_t randi54 (void)
368  {
369  const uint32_t lo = randi32 ();
370  const uint32_t hi = randi32 () & 0x3FFFFF;
371 #if defined (HAVE_X86_32)
372  uint64_t u;
373  uint32_t *p = static_cast<uint32_t *> (&u);
374  p[0] = lo;
375  p[1] = hi;
376  return u;
377 #else
378  return ((static_cast<uint64_t> (hi) << 32) | lo);
379 #endif
380  }
381 
382  /* generates a random number on (0,1)-real-interval */
383  static float randu24 (void)
384  {
385  uint32_t i;
386 
387  do
388  {
389  i = randi32 () & static_cast<uint32_t> (0xFFFFFF);
390  }
391  while (i == 0);
392 
393  return i * (1.0f / 16777216.0f);
394  }
395 
396  /* generates a random number on (0,1) with 53-bit resolution */
397  static double randu53 (void)
398  {
399  int32_t a, b;
400 
401  do
402  {
403  a = randi32 () >> 5;
404  b = randi32 () >> 6;
405  }
406  while (a == 0 && b == 0);
407 
408  return (a*67108864.0 + b) * (1.0/9007199254740992.0);
409  }
410 
411  /* Determine mantissa for uniform doubles */
412  template <>
413  double
414  rand_uniform<double> (void)
415  {
416  return randu53 ();
417  }
418 
419  /* Determine mantissa for uniform floats */
420  template <>
421  float
422  rand_uniform<float> (void)
423  {
424  return randu24 ();
425  }
426 
427  /* ===== Ziggurat normal and exponential generators ===== */
428 
429 #define ZIGGURAT_TABLE_SIZE 256
430 
431 #define ZIGGURAT_NOR_R 3.6541528853610088
432 #define ZIGGURAT_NOR_INV_R 0.27366123732975828
433 #define NOR_SECTION_AREA 0.00492867323399
434 
435 #define ZIGGURAT_EXP_R 7.69711747013104972
436 #define ZIGGURAT_EXP_INV_R 0.129918765548341586
437 #define EXP_SECTION_AREA 0.0039496598225815571993
438 
439 #define ZIGINT uint64_t
440 #define EMANTISSA 9007199254740992.0 /* 53 bit mantissa */
441 #define ERANDI randi53() /* 53 bits for mantissa */
442 #define NMANTISSA EMANTISSA
443 #define NRANDI randi54() /* 53 bits for mantissa + 1 bit sign */
444 #define RANDU randu53()
445 
450 
451  /*
452  This code is based on the paper Marsaglia and Tsang, "The ziggurat method
453  for generating random variables", Journ. Statistical Software. Code was
454  presented in this paper for a Ziggurat of 127 levels and using a 32 bit
455  integer random number generator. This version of the code, uses the
456  Mersenne Twister as the integer generator and uses 256 levels in the
457  Ziggurat. This has several advantages.
458 
459  1) As Marsaglia and Tsang themselves states, the more levels the few
460  times the expensive tail algorithm must be called
461  2) The cycle time of the generator is determined by the integer
462  generator, thus the use of a Mersenne Twister for the core random
463  generator makes this cycle extremely long.
464  3) The license on the original code was unclear, thus rewriting the code
465  from the article means we are free of copyright issues.
466  4) Compile flag for full 53-bit random mantissa.
467 
468  It should be stated that the authors made my life easier, by the fact that
469  the algorithm developed in the text of the article is for a 256 level
470  ziggurat, even if the code itself isn't...
471 
472  One modification to the algorithm developed in the article, is that it is
473  assumed that 0 <= x < Inf, and "unsigned long"s are used, thus resulting in
474  terms like 2^32 in the code. As the normal distribution is defined between
475  -Inf < x < Inf, we effectively only have 31 bit integers plus a sign. Thus
476  in Marsaglia and Tsang, terms like 2^32 become 2^31. We use NMANTISSA for
477  this term. The exponential distribution is one sided so we use the
478  full 32 bits. We use EMANTISSA for this term.
479 
480  It appears that I'm slightly slower than the code in the article, this
481  is partially due to a better generator of random integers than they
482  use. But might also be that the case of rapid return was optimized by
483  inlining the relevant code with a #define. As the basic Mersenne
484  Twister is only 25% faster than this code I suspect that the main
485  reason is just the use of the Mersenne Twister and not the inlining,
486  so I'm not going to try and optimize further.
487  */
488 
490  {
491  int i;
492  double x, x1;
493 
494  /* Ziggurat tables for the normal distribution */
495  x1 = ZIGGURAT_NOR_R;
496  wi[255] = x1 / NMANTISSA;
497  fi[255] = exp (-0.5 * x1 * x1);
498 
499  /* Index zero is special for tail strip, where Marsaglia and Tsang
500  * defines this as
501  * k_0 = 2^31 * r * f(r) / v, w_0 = 0.5^31 * v / f(r), f_0 = 1,
502  * where v is the area of each strip of the ziggurat.
503  */
504  ki[0] = static_cast<ZIGINT> (x1 * fi[255] / NOR_SECTION_AREA * NMANTISSA);
505  wi[0] = NOR_SECTION_AREA / fi[255] / NMANTISSA;
506  fi[0] = 1.;
507 
508  for (i = 254; i > 0; i--)
509  {
510  /* New x is given by x = f^{-1}(v/x_{i+1} + f(x_{i+1})), thus
511  * need inverse operator of y = exp(-0.5*x*x) -> x = sqrt(-2*ln(y))
512  */
513  x = std::sqrt (-2. * std::log (NOR_SECTION_AREA / x1 + fi[i+1]));
514  ki[i+1] = static_cast<ZIGINT> (x / x1 * NMANTISSA);
515  wi[i] = x / NMANTISSA;
516  fi[i] = exp (-0.5 * x * x);
517  x1 = x;
518  }
519 
520  ki[1] = 0;
521 
522  /* Zigurrat tables for the exponential distribution */
523  x1 = ZIGGURAT_EXP_R;
524  we[255] = x1 / EMANTISSA;
525  fe[255] = exp (-x1);
526 
527  /* Index zero is special for tail strip, where Marsaglia and Tsang
528  * defines this as
529  * k_0 = 2^32 * r * f(r) / v, w_0 = 0.5^32 * v / f(r), f_0 = 1,
530  * where v is the area of each strip of the ziggurat.
531  */
532  ke[0] = static_cast<ZIGINT> (x1 * fe[255] / EXP_SECTION_AREA * EMANTISSA);
533  we[0] = EXP_SECTION_AREA / fe[255] / EMANTISSA;
534  fe[0] = 1.;
535 
536  for (i = 254; i > 0; i--)
537  {
538  /* New x is given by x = f^{-1}(v/x_{i+1} + f(x_{i+1})), thus
539  * need inverse operator of y = exp(-x) -> x = -ln(y)
540  */
541  x = - std::log (EXP_SECTION_AREA / x1 + fe[i+1]);
542  ke[i+1] = static_cast<ZIGINT> (x / x1 * EMANTISSA);
543  we[i] = x / EMANTISSA;
544  fe[i] = exp (-x);
545  x1 = x;
546  }
547  ke[1] = 0;
548 
549  initt = 0;
550  }
551 
552  /*
553  * Here is the guts of the algorithm. As Marsaglia and Tsang state the
554  * algorithm in their paper
555  *
556  * 1) Calculate a random signed integer j and let i be the index
557  * provided by the rightmost 8-bits of j
558  * 2) Set x = j * w_i. If j < k_i return x
559  * 3) If i = 0, then return x from the tail
560  * 4) If [f(x_{i-1}) - f(x_i)] * U < f(x) - f(x_i), return x
561  * 5) goto step 1
562  *
563  * Where f is the functional form of the distribution, which for a normal
564  * distribution is exp(-0.5*x*x)
565  */
566 
567 
568  template <> double rand_normal<double> (void)
569  {
570  if (initt)
572 
573  while (1)
574  {
575  /* The following code is specialized for 32-bit mantissa.
576  * Compared to the arbitrary mantissa code, there is a performance
577  * gain for 32-bits: PPC: 2%, MIPS: 8%, x86: 40%
578  * There is a bigger performance gain compared to using a full
579  * 53-bit mantissa: PPC: 60%, MIPS: 65%, x86: 240%
580  * Of course, different compilers and operating systems may
581  * have something to do with this.
582  */
583 # if defined (HAVE_X86_32)
584  /* 53-bit mantissa, 1-bit sign, x86 32-bit architecture */
585  double x;
586  int si,idx;
587  uint32_t lo, hi;
588  int64_t rabs;
589  uint32_t *p = (uint32_t *)&rabs;
590  lo = randi32 ();
591  idx = lo & 0xFF;
592  hi = randi32 ();
593  si = hi & UMASK;
594  p[0] = lo;
595  p[1] = hi & 0x1FFFFF;
596  x = ( si ? -rabs : rabs ) * wi[idx];
597 # else
598  /* arbitrary mantissa (selected by NRANDI, with 1 bit for sign) */
599  const uint64_t r = NRANDI;
600  const int64_t rabs = r >> 1;
601  const int idx = static_cast<int> (rabs & 0xFF);
602  const double x = ( (r & 1) ? -rabs : rabs) * wi[idx];
603 # endif
604  if (rabs < static_cast<int64_t> (ki[idx]))
605  return x; /* 99.3% of the time we return here 1st try */
606  else if (idx == 0)
607  {
608  /* As stated in Marsaglia and Tsang
609  *
610  * For the normal tail, the method of Marsaglia[5] provides:
611  * generate x = -ln(U_1)/r, y = -ln(U_2), until y+y > x*x,
612  * then return r+x. Except that r+x is always in the positive
613  * tail!!!! Any thing random might be used to determine the
614  * sign, but as we already have r we might as well use it
615  *
616  * [PAK] but not the bottom 8 bits, since they are all 0 here!
617  */
618  double xx, yy;
619  do
620  {
621  xx = - ZIGGURAT_NOR_INV_R * std::log (RANDU);
622  yy = - std::log (RANDU);
623  }
624  while ( yy+yy <= xx*xx);
625  return ((rabs & 0x100) ? -ZIGGURAT_NOR_R-xx : ZIGGURAT_NOR_R+xx);
626  }
627  else if ((fi[idx-1] - fi[idx]) * RANDU + fi[idx] < exp (-0.5*x*x))
628  return x;
629  }
630  }
631 
632  template <> double rand_exponential<double> (void)
633  {
634  if (initt)
636 
637  while (1)
638  {
639  ZIGINT ri = ERANDI;
640  const int idx = static_cast<int> (ri & 0xFF);
641  const double x = ri * we[idx];
642  if (ri < ke[idx])
643  return x; /* 98.9% of the time we return here 1st try */
644  else if (idx == 0)
645  {
646  /* As stated in Marsaglia and Tsang
647  *
648  * For the exponential tail, the method of Marsaglia[5] provides:
649  * x = r - ln(U);
650  */
651  return ZIGGURAT_EXP_R - std::log (RANDU);
652  }
653  else if ((fe[idx-1] - fe[idx]) * RANDU + fe[idx] < exp (-x))
654  return x;
655  }
656  }
657 
658  template <> void rand_uniform<double> (octave_idx_type n, double *p)
659  {
660  std::generate_n (p, n, [](void) { return rand_uniform<double> (); });
661  }
662 
663  template <> void rand_normal (octave_idx_type n, double *p)
664  {
665  std::generate_n (p, n, [](void) { return rand_normal<double> (); });
666  }
667 
668  template <> void rand_exponential (octave_idx_type n, double *p)
669  {
670  std::generate_n (p, n, [](void) { return rand_exponential<double> (); });
671  }
672 
673 #undef ZIGINT
674 #undef EMANTISSA
675 #undef ERANDI
676 #undef NMANTISSA
677 #undef NRANDI
678 #undef RANDU
679 
680 #define ZIGINT uint32_t
681 #define EMANTISSA 4294967296.0 /* 32 bit mantissa */
682 #define ERANDI randi32() /* 32 bits for mantissa */
683 #define NMANTISSA 2147483648.0 /* 31 bit mantissa */
684 #define NRANDI randi32() /* 31 bits for mantissa + 1 bit sign */
685 #define RANDU randu24()
686 
691 
692  static void create_ziggurat_float_tables (void)
693  {
694  int i;
695  float x, x1;
696 
697  /* Ziggurat tables for the normal distribution */
698  x1 = ZIGGURAT_NOR_R;
699  fwi[255] = x1 / NMANTISSA;
700  ffi[255] = exp (-0.5 * x1 * x1);
701 
702  /* Index zero is special for tail strip, where Marsaglia and Tsang
703  * defines this as
704  * k_0 = 2^31 * r * f(r) / v, w_0 = 0.5^31 * v / f(r), f_0 = 1,
705  * where v is the area of each strip of the ziggurat.
706  */
707  fki[0] = static_cast<ZIGINT> (x1 * ffi[255] / NOR_SECTION_AREA * NMANTISSA);
708  fwi[0] = NOR_SECTION_AREA / ffi[255] / NMANTISSA;
709  ffi[0] = 1.;
710 
711  for (i = 254; i > 0; i--)
712  {
713  /* New x is given by x = f^{-1}(v/x_{i+1} + f(x_{i+1})), thus
714  * need inverse operator of y = exp(-0.5*x*x) -> x = sqrt(-2*ln(y))
715  */
716  x = std::sqrt (-2. * std::log (NOR_SECTION_AREA / x1 + ffi[i+1]));
717  fki[i+1] = static_cast<ZIGINT> (x / x1 * NMANTISSA);
718  fwi[i] = x / NMANTISSA;
719  ffi[i] = exp (-0.5 * x * x);
720  x1 = x;
721  }
722 
723  fki[1] = 0;
724 
725  /* Zigurrat tables for the exponential distribution */
726  x1 = ZIGGURAT_EXP_R;
727  fwe[255] = x1 / EMANTISSA;
728  ffe[255] = exp (-x1);
729 
730  /* Index zero is special for tail strip, where Marsaglia and Tsang
731  * defines this as
732  * k_0 = 2^32 * r * f(r) / v, w_0 = 0.5^32 * v / f(r), f_0 = 1,
733  * where v is the area of each strip of the ziggurat.
734  */
735  fke[0] = static_cast<ZIGINT> (x1 * ffe[255] / EXP_SECTION_AREA * EMANTISSA);
736  fwe[0] = EXP_SECTION_AREA / ffe[255] / EMANTISSA;
737  ffe[0] = 1.;
738 
739  for (i = 254; i > 0; i--)
740  {
741  /* New x is given by x = f^{-1}(v/x_{i+1} + f(x_{i+1})), thus
742  * need inverse operator of y = exp(-x) -> x = -ln(y)
743  */
744  x = - std::log (EXP_SECTION_AREA / x1 + ffe[i+1]);
745  fke[i+1] = static_cast<ZIGINT> (x / x1 * EMANTISSA);
746  fwe[i] = x / EMANTISSA;
747  ffe[i] = exp (-x);
748  x1 = x;
749  }
750  fke[1] = 0;
751 
752  inittf = 0;
753  }
754 
755  /*
756  * Here is the guts of the algorithm. As Marsaglia and Tsang state the
757  * algorithm in their paper
758  *
759  * 1) Calculate a random signed integer j and let i be the index
760  * provided by the rightmost 8-bits of j
761  * 2) Set x = j * w_i. If j < k_i return x
762  * 3) If i = 0, then return x from the tail
763  * 4) If [f(x_{i-1}) - f(x_i)] * U < f(x) - f(x_i), return x
764  * 5) goto step 1
765  *
766  * Where f is the functional form of the distribution, which for a normal
767  * distribution is exp(-0.5*x*x)
768  */
769 
770  template <> float rand_normal<float> (void)
771  {
772  if (inittf)
774 
775  while (1)
776  {
777  /* 32-bit mantissa */
778  const uint32_t r = randi32 ();
779  const uint32_t rabs = r & LMASK;
780  const int idx = static_cast<int> (r & 0xFF);
781  const float x = static_cast<int32_t> (r) * fwi[idx];
782  if (rabs < fki[idx])
783  return x; /* 99.3% of the time we return here 1st try */
784  else if (idx == 0)
785  {
786  /* As stated in Marsaglia and Tsang
787  *
788  * For the normal tail, the method of Marsaglia[5] provides:
789  * generate x = -ln(U_1)/r, y = -ln(U_2), until y+y > x*x,
790  * then return r+x. Except that r+x is always in the positive
791  * tail!!!! Any thing random might be used to determine the
792  * sign, but as we already have r we might as well use it
793  *
794  * [PAK] but not the bottom 8 bits, since they are all 0 here!
795  */
796  float xx, yy;
797  do
798  {
799  xx = - ZIGGURAT_NOR_INV_R * std::log (RANDU);
800  yy = - std::log (RANDU);
801  }
802  while ( yy+yy <= xx*xx);
803  return ((rabs & 0x100) ? -ZIGGURAT_NOR_R-xx : ZIGGURAT_NOR_R+xx);
804  }
805  else if ((ffi[idx-1] - ffi[idx]) * RANDU + ffi[idx] < exp (-0.5*x*x))
806  return x;
807  }
808  }
809 
810  template <> float rand_exponential<float> (void)
811  {
812  if (inittf)
814 
815  while (1)
816  {
817  ZIGINT ri = ERANDI;
818  const int idx = static_cast<int> (ri & 0xFF);
819  const float x = ri * fwe[idx];
820  if (ri < fke[idx])
821  return x; /* 98.9% of the time we return here 1st try */
822  else if (idx == 0)
823  {
824  /* As stated in Marsaglia and Tsang
825  *
826  * For the exponential tail, the method of Marsaglia[5] provides:
827  * x = r - ln(U);
828  */
829  return ZIGGURAT_EXP_R - std::log (RANDU);
830  }
831  else if ((ffe[idx-1] - ffe[idx]) * RANDU + ffe[idx] < exp (-x))
832  return x;
833  }
834  }
835 
836  template <> void rand_uniform (octave_idx_type n, float *p)
837  {
838  std::generate_n (p, n, [](void) { return rand_uniform<float> (); });
839  }
840 
841  template <> void rand_normal (octave_idx_type n, float *p)
842  {
843  std::generate_n (p, n, [](void) { return rand_normal<float> (); });
844  }
845 
846  template <> void rand_exponential (octave_idx_type n, float *p)
847  {
848  std::generate_n (p, n, [](void) { return rand_exponential<float> (); });
849  }
850 }
851 
long usec(void) const
Definition: oct-time.h:115
time_t unix_time(void) const
Definition: oct-time.h:113
F77_RET_T const F77_DBLE * x
octave_idx_type n
Definition: mx-inlines.cc:753
T * r
Definition: mx-inlines.cc:773
std::FILE * fopen(const std::string &filename, const std::string &mode)
Definition: lo-sysdep.cc:295
static uint64_t randi53(void)
Definition: randmtzig.cc:352
static float randu24(void)
Definition: randmtzig.cc:383
static uint32_t state[624]
Definition: randmtzig.cc:190
double rand_normal< double >(void)
Definition: randmtzig.cc:568
static double we[256]
Definition: randmtzig.cc:449
static uint32_t randmt(void)
Definition: randmtzig.cc:332
static uint64_t randi54(void)
Definition: randmtzig.cc:367
void set_mersenne_twister_state(const uint32_t *save)
Definition: randmtzig.cc:294
static float fwi[256]
Definition: randmtzig.cc:688
static float ffi[256]
Definition: randmtzig.cc:688
static int inittf
Definition: randmtzig.cc:194
static void create_ziggurat_float_tables(void)
Definition: randmtzig.cc:692
static uint32_t fke[256]
Definition: randmtzig.cc:689
float rand_normal< float >(void)
Definition: randmtzig.cc:770
double rand_exponential< double >(void)
Definition: randmtzig.cc:632
static uint64_t ki[256]
Definition: randmtzig.cc:446
static uint32_t * next
Definition: randmtzig.cc:189
static int left
Definition: randmtzig.cc:191
static int initf
Definition: randmtzig.cc:192
static double wi[256]
Definition: randmtzig.cc:447
float rand_exponential< float >(void)
Definition: randmtzig.cc:810
static uint32_t fki[256]
Definition: randmtzig.cc:687
static float ffe[256]
Definition: randmtzig.cc:690
void init_mersenne_twister(const uint32_t s)
Definition: randmtzig.cc:197
static void next_state(void)
Definition: randmtzig.cc:307
static double fe[256]
Definition: randmtzig.cc:449
float rand_uniform< float >(void)
Definition: randmtzig.cc:422
void rand_exponential(octave_idx_type n, double *p)
Definition: randmtzig.cc:668
void create_ziggurat_tables(void)
Definition: randmtzig.cc:489
void rand_normal(octave_idx_type n, double *p)
Definition: randmtzig.cc:663
static uint64_t ke[256]
Definition: randmtzig.cc:448
void rand_uniform(octave_idx_type n, float *p)
Definition: randmtzig.cc:836
static int initt
Definition: randmtzig.cc:193
static float fwe[256]
Definition: randmtzig.cc:690
void get_mersenne_twister_state(uint32_t *save)
Definition: randmtzig.cc:301
static double fi[256]
Definition: randmtzig.cc:447
double rand_uniform< double >(void)
Definition: randmtzig.cc:414
static double randu53(void)
Definition: randmtzig.cc:397
#define TWIST(u, v)
Definition: randmtzig.cc:187
#define ZIGGURAT_EXP_R
Definition: randmtzig.cc:435
#define EMANTISSA
Definition: randmtzig.cc:681
#define ZIGGURAT_TABLE_SIZE
Definition: randmtzig.cc:429
#define randi32
Definition: randmtzig.cc:350
#define RANDU
Definition: randmtzig.cc:685
#define ZIGINT
Definition: randmtzig.cc:680
#define LMASK
Definition: randmtzig.cc:185
#define NRANDI
Definition: randmtzig.cc:684
#define ERANDI
Definition: randmtzig.cc:682
#define NOR_SECTION_AREA
Definition: randmtzig.cc:433
#define ZIGGURAT_NOR_R
Definition: randmtzig.cc:431
#define ZIGGURAT_NOR_INV_R
Definition: randmtzig.cc:432
#define UMASK
Definition: randmtzig.cc:184
#define NMANTISSA
Definition: randmtzig.cc:683
#define MT_M
Definition: randmtzig.cc:182
#define EXP_SECTION_AREA
Definition: randmtzig.cc:437
#define MT_N
Definition: randmtzig.h:72