randmtzig.c

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2006-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 /*
00024    A C-program for MT19937, with initialization improved 2002/2/10.
00025    Coded by Takuji Nishimura and Makoto Matsumoto.
00026    This is a faster version by taking Shawn Cokus's optimization,
00027    Matthe Bellew's simplification, Isaku Wada's real version.
00028    David Bateman added normal and exponential distributions following
00029    Marsaglia and Tang's Ziggurat algorithm.
00030 
00031    Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
00032    Copyright (C) 2004, David Bateman
00033    All rights reserved.
00034 
00035    Redistribution and use in source and binary forms, with or without
00036    modification, are permitted provided that the following conditions
00037    are met:
00038 
00039      1. Redistributions of source code must retain the above copyright
00040         notice, this list of conditions and the following disclaimer.
00041 
00042      2. Redistributions in binary form must reproduce the above copyright
00043         notice, this list of conditions and the following disclaimer in the
00044         documentation and/or other materials provided with the distribution.
00045 
00046      3. The names of its contributors may not be used to endorse or promote
00047         products derived from this software without specific prior written
00048         permission.
00049 
00050    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00051    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00052    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00053    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER
00054    OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00055    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00056    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00057    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00058    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00059    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00060    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00061 
00062 
00063    Any feedback is very welcome.
00064    http://www.math.keio.ac.jp/matumoto/emt.html
00065    email: matumoto@math.keio.ac.jp
00066 
00067    * 2006-04-01 David Bateman
00068    * * convert for use in octave, declaring static functions only used
00069    *   here and adding oct_ to functions visible externally
00070    * * inverse sense of ALLBITS
00071    * 2004-01-19 Paul Kienzle
00072    * * comment out main
00073    * add init_by_entropy, get_state, set_state
00074    * * converted to allow compiling by C++ compiler
00075    *
00076    * 2004-01-25 David Bateman
00077    * * Add Marsaglia and Tsang Ziggurat code
00078    *
00079    * 2004-07-13 Paul Kienzle
00080    * * make into an independent library with some docs.
00081    * * introduce new main and test code.
00082    *
00083    * 2004-07-28 Paul Kienzle & David Bateman
00084    * * add -DALLBITS flag for 32 vs. 53 bits of randomness in mantissa
00085    * * make the naming scheme more uniform
00086    * * add -DHAVE_X86 for faster support of 53 bit mantissa on x86 arch.
00087    *
00088    * 2005-02-23 Paul Kienzle
00089    * * fix -DHAVE_X86_32 flag and add -DUSE_X86_32=0|1 for explicit control
00090    */
00091 
00092 /*
00093    === Build instructions ===
00094 
00095    Compile with -DHAVE_GETTIMEOFDAY if the gettimeofday function is
00096    available.  This is not necessary if your architecture has
00097    /dev/urandom defined.
00098 
00099    Compile with -DALLBITS to disable 53-bit random numbers. This is about
00100    50% slower than using 32-bit random numbers.
00101 
00102    Uses implicit -Di386 or explicit -DHAVE_X86_32 to determine if CPU=x86.
00103    You can force X86 behaviour with -DUSE_X86_32=1, or suppress it with
00104    -DUSE_X86_32=0. You should also consider -march=i686 or similar for
00105    extra performance. Check whether -DUSE_X86_32=0 is faster on 64-bit
00106    x86 architectures.
00107 
00108    If you want to replace the Mersenne Twister with another
00109    generator then redefine randi32 appropriately.
00110 
00111    === Usage instructions ===
00112    Before using any of the generators, initialize the state with one of
00113    oct_init_by_int, oct_init_by_array or oct_init_by_entropy.
00114 
00115    All generators share the same state vector.
00116 
00117    === Mersenne Twister ===
00118    void oct_init_by_int(uint32_t s)           32-bit initial state
00119    void oct_init_by_array(uint32_t k[],int m) m*32-bit initial state
00120    void oct_init_by_entropy(void)             random initial state
00121    void oct_get_state(uint32_t save[MT_N+1])  saves state in array
00122    void oct_set_state(uint32_t save[MT_N+1])  restores state from array
00123    static uint32_t randmt(void)               returns 32-bit unsigned int
00124 
00125    === inline generators ===
00126    static uint32_t randi32(void)   returns 32-bit unsigned int
00127    static uint64_t randi53(void)   returns 53-bit unsigned int
00128    static uint64_t randi54(void)   returns 54-bit unsigned int
00129    static uint64_t randi64(void)   returns 64-bit unsigned int
00130    static double randu32(void)     returns 32-bit uniform in (0,1)
00131    static double randu53(void)     returns 53-bit uniform in (0,1)
00132 
00133    double oct_randu(void)       returns M-bit uniform in (0,1)
00134    double oct_randn(void)       returns M-bit standard normal
00135    double oct_rande(void)       returns N-bit standard exponential
00136 
00137    === Array generators ===
00138    void oct_fill_randi32(octave_idx_type, uint32_t [])
00139    void oct_fill_randi64(octave_idx_type, uint64_t [])
00140    void oct_fill_randu(octave_idx_type, double [])
00141    void oct_fill_randn(octave_idx_type, double [])
00142    void oct_fill_rande(octave_idx_type, double [])
00143 
00144 */
00145 
00146 #if defined (HAVE_CONFIG_H)
00147 #include <config.h>
00148 #endif
00149 
00150 #include <stdio.h>
00151 #include <time.h>
00152 
00153 #ifdef HAVE_GETTIMEOFDAY
00154 #include <sys/time.h>
00155 #endif
00156 
00157 #include "lo-math.h"
00158 #include "randmtzig.h"
00159 
00160 /* FIXME may want to suppress X86 if sizeof(long)>4 */
00161 #if !defined(USE_X86_32)
00162 # if defined(i386) || defined(HAVE_X86_32)
00163 #  define USE_X86_32 1
00164 # else
00165 #  define USE_X86_32 0
00166 # endif
00167 #endif
00168 
00169 /* ===== Mersenne Twister 32-bit generator ===== */
00170 
00171 #define MT_M 397
00172 #define MATRIX_A 0x9908b0dfUL   /* constant vector a */
00173 #define UMASK 0x80000000UL /* most significant w-r bits */
00174 #define LMASK 0x7fffffffUL /* least significant r bits */
00175 #define MIXBITS(u,v) ( ((u) & UMASK) | ((v) & LMASK) )
00176 #define TWIST(u,v) ((MIXBITS(u,v) >> 1) ^ ((v)&1UL ? MATRIX_A : 0UL))
00177 
00178 static uint32_t *next;
00179 static uint32_t state[MT_N]; /* the array for the state vector  */
00180 static int left = 1;
00181 static int initf = 0;
00182 static int initt = 1;
00183 
00184 /* initializes state[MT_N] with a seed */
00185 void
00186 oct_init_by_int (uint32_t s)
00187 {
00188     int j;
00189     state[0] = s & 0xffffffffUL;
00190     for (j = 1; j < MT_N; j++) {
00191         state[j] = (1812433253UL * (state[j-1] ^ (state[j-1] >> 30)) + j);
00192         /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
00193         /* In the previous versions, MSBs of the seed affect   */
00194         /* only MSBs of the array state[].                        */
00195         /* 2002/01/09 modified by Makoto Matsumoto             */
00196         state[j] &= 0xffffffffUL;  /* for >32 bit machines */
00197     }
00198     left = 1;
00199     initf = 1;
00200 }
00201 
00202 /* initialize by an array with array-length */
00203 /* init_key is the array for initializing keys */
00204 /* key_length is its length */
00205 void
00206 oct_init_by_array (uint32_t *init_key, int key_length)
00207 {
00208   int i, j, k;
00209   oct_init_by_int (19650218UL);
00210   i = 1;
00211   j = 0;
00212   k = (MT_N > key_length ? MT_N : key_length);
00213   for (; k; k--)
00214     {
00215       state[i] = (state[i] ^ ((state[i-1] ^ (state[i-1] >> 30)) * 1664525UL))
00216         + init_key[j] + j; /* non linear */
00217       state[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
00218       i++;
00219       j++;
00220       if (i >= MT_N)
00221         {
00222           state[0] = state[MT_N-1];
00223           i = 1;
00224         }
00225       if (j >= key_length)
00226         j = 0;
00227     }
00228   for (k = MT_N - 1; k; k--)
00229     {
00230       state[i] = (state[i] ^ ((state[i-1] ^ (state[i-1] >> 30)) * 1566083941UL))
00231         - i; /* non linear */
00232       state[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
00233       i++;
00234       if (i >= MT_N)
00235         {
00236           state[0] = state[MT_N-1];
00237           i = 1;
00238         }
00239     }
00240 
00241   state[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
00242   left = 1;
00243   initf = 1;
00244 }
00245 
00246 void
00247 oct_init_by_entropy (void)
00248 {
00249     uint32_t entropy[MT_N];
00250     int n = 0;
00251 
00252     /* Look for entropy in /dev/urandom */
00253     FILE* urandom =fopen("/dev/urandom", "rb");
00254     if (urandom)
00255       {
00256         while (n < MT_N)
00257           {
00258             unsigned char word[4];
00259             if (fread(word, 4, 1, urandom) != 1)
00260               break;
00261             entropy[n++] = word[0]+(word[1]<<8)+(word[2]<<16)+(word[3]<<24);
00262           }
00263         fclose(urandom);
00264       }
00265 
00266     /* If there isn't enough entropy, gather some from various sources */
00267     if (n < MT_N)
00268       entropy[n++] = time(NULL); /* Current time in seconds */
00269     if (n < MT_N)
00270       entropy[n++] = clock();    /* CPU time used (usec) */
00271 #ifdef HAVE_GETTIMEOFDAY
00272     if (n < MT_N)
00273       {
00274         struct timeval tv;
00275         if (gettimeofday(&tv, NULL) != -1)
00276           entropy[n++] = tv.tv_usec;   /* Fractional part of current time */
00277       }
00278 #endif
00279     /* Send all the entropy into the initial state vector */
00280     oct_init_by_array(entropy,n);
00281 }
00282 
00283 void
00284 oct_set_state (uint32_t *save)
00285 {
00286   int i;
00287   for (i = 0; i < MT_N; i++)
00288     state[i] = save[i];
00289   left = save[MT_N];
00290   next = state + (MT_N - left + 1);
00291 }
00292 
00293 void
00294 oct_get_state (uint32_t *save)
00295 {
00296   int i;
00297   for (i = 0; i < MT_N; i++)
00298     save[i] = state[i];
00299   save[MT_N] = left;
00300 }
00301 
00302 static void
00303 next_state (void)
00304 {
00305   uint32_t *p = state;
00306   int j;
00307 
00308   /* if init_by_int() has not been called, */
00309   /* a default initial seed is used         */
00310   /* if (initf==0) init_by_int(5489UL); */
00311   /* Or better yet, a random seed! */
00312   if (initf == 0)
00313     oct_init_by_entropy();
00314 
00315   left = MT_N;
00316   next = state;
00317 
00318   for (j = MT_N - MT_M + 1; --j; p++)
00319     *p = p[MT_M] ^ TWIST(p[0], p[1]);
00320 
00321   for (j = MT_M; --j; p++)
00322     *p = p[MT_M-MT_N] ^ TWIST(p[0], p[1]);
00323 
00324   *p = p[MT_M-MT_N] ^ TWIST(p[0], state[0]);
00325 }
00326 
00327 /* generates a random number on [0,0xffffffff]-interval */
00328 static uint32_t
00329 randmt (void)
00330 {
00331   register uint32_t y;
00332 
00333   if (--left == 0)
00334     next_state();
00335   y = *next++;
00336 
00337   /* Tempering */
00338   y ^= (y >> 11);
00339   y ^= (y << 7) & 0x9d2c5680UL;
00340   y ^= (y << 15) & 0xefc60000UL;
00341   return (y ^ (y >> 18));
00342 }
00343 
00344 /* ===== Uniform generators ===== */
00345 
00346 /* Select which 32 bit generator to use */
00347 #define randi32 randmt
00348 
00349 static uint64_t
00350 randi53 (void)
00351 {
00352   const uint32_t lo = randi32();
00353   const uint32_t hi = randi32()&0x1FFFFF;
00354 #if HAVE_X86_32
00355   uint64_t u;
00356   uint32_t *p = (uint32_t *)&u;
00357   p[0] = lo;
00358   p[1] = hi;
00359   return u;
00360 #else
00361   return (((uint64_t)hi<<32)|lo);
00362 #endif
00363 }
00364 
00365 static uint64_t
00366 randi54 (void)
00367 {
00368   const uint32_t lo = randi32();
00369   const uint32_t hi = randi32()&0x3FFFFF;
00370 #if HAVE_X86_32
00371   uint64_t u;
00372   uint32_t *p = (uint32_t *)&u;
00373   p[0] = lo;
00374   p[1] = hi;
00375   return u;
00376 #else
00377   return (((uint64_t)hi<<32)|lo);
00378 #endif
00379 }
00380 
00381 #if 0
00382 // FIXME -- this doesn't seem to be used anywhere; should it be removed?
00383 static uint64_t
00384 randi64 (void)
00385 {
00386   const uint32_t lo = randi32();
00387   const uint32_t hi = randi32();
00388 #if HAVE_X86_32
00389   uint64_t u;
00390   uint32_t *p = (uint32_t *)&u;
00391   p[0] = lo;
00392   p[1] = hi;
00393   return u;
00394 #else
00395   return (((uint64_t)hi<<32)|lo);
00396 #endif
00397 }
00398 #endif
00399 
00400 #ifdef ALLBITS
00401 /* generates a random number on (0,1)-real-interval */
00402 static double
00403 randu32 (void)
00404 {
00405   return ((double)randi32() + 0.5) * (1.0/4294967296.0);
00406   /* divided by 2^32 */
00407 }
00408 #else
00409 /* generates a random number on (0,1) with 53-bit resolution */
00410 static double
00411 randu53 (void)
00412 {
00413   const uint32_t a=randi32()>>5;
00414   const uint32_t b=randi32()>>6;
00415   return (a*67108864.0+b+0.4) * (1.0/9007199254740992.0);
00416 }
00417 #endif
00418 
00419 /* Determine mantissa for uniform doubles */
00420 double
00421 oct_randu (void)
00422 {
00423 #ifdef ALLBITS
00424   return randu32 ();
00425 #else
00426   return randu53 ();
00427 #endif
00428 }
00429 
00430 /* ===== Ziggurat normal and exponential generators ===== */
00431 #ifdef ALLBITS
00432 # define ZIGINT uint32_t
00433 # define EMANTISSA 4294967296.0 /* 32 bit mantissa */
00434 # define ERANDI randi32() /* 32 bits for mantissa */
00435 # define NMANTISSA 2147483648.0 /* 31 bit mantissa */
00436 # define NRANDI randi32() /* 31 bits for mantissa + 1 bit sign */
00437 # define RANDU randu32()
00438 #else
00439 # define ZIGINT uint64_t
00440 # define EMANTISSA 9007199254740992.0  /* 53 bit mantissa */
00441 # define ERANDI randi53() /* 53 bits for mantissa */
00442 # define NMANTISSA EMANTISSA
00443 # define NRANDI randi54() /* 53 bits for mantissa + 1 bit sign */
00444 # define RANDU randu53()
00445 #endif
00446 
00447 #define ZIGGURAT_TABLE_SIZE 256
00448 
00449 #define ZIGGURAT_NOR_R 3.6541528853610088
00450 #define ZIGGURAT_NOR_INV_R 0.27366123732975828
00451 #define NOR_SECTION_AREA 0.00492867323399
00452 
00453 #define ZIGGURAT_EXP_R 7.69711747013104972
00454 #define ZIGGURAT_EXP_INV_R 0.129918765548341586
00455 #define EXP_SECTION_AREA 0.0039496598225815571993
00456 
00457 static ZIGINT ki[ZIGGURAT_TABLE_SIZE];
00458 static double wi[ZIGGURAT_TABLE_SIZE], fi[ZIGGURAT_TABLE_SIZE];
00459 static ZIGINT ke[ZIGGURAT_TABLE_SIZE];
00460 static double we[ZIGGURAT_TABLE_SIZE], fe[ZIGGURAT_TABLE_SIZE];
00461 
00462 /*
00463 This code is based on the paper Marsaglia and Tsang, "The ziggurat method
00464 for generating random variables", Journ. Statistical Software. Code was
00465 presented in this paper for a Ziggurat of 127 levels and using a 32 bit
00466 integer random number generator. This version of the code, uses the
00467 Mersenne Twister as the integer generator and uses 256 levels in the
00468 Ziggurat. This has several advantages.
00469 
00470   1) As Marsaglia and Tsang themselves states, the more levels the few
00471      times the expensive tail algorithm must be called
00472   2) The cycle time of the generator is determined by the integer
00473      generator, thus the use of a Mersenne Twister for the core random
00474      generator makes this cycle extremely long.
00475   3) The license on the original code was unclear, thus rewriting the code
00476      from the article means we are free of copyright issues.
00477   4) Compile flag for full 53-bit random mantissa.
00478 
00479 It should be stated that the authors made my life easier, by the fact that
00480 the algorithm developed in the text of the article is for a 256 level
00481 ziggurat, even if the code itself isn't...
00482 
00483 One modification to the algorithm developed in the article, is that it is
00484 assumed that 0 <= x < Inf, and "unsigned long"s are used, thus resulting in
00485 terms like 2^32 in the code. As the normal distribution is defined between
00486 -Inf < x < Inf, we effectively only have 31 bit integers plus a sign. Thus
00487 in Marsaglia and Tsang, terms like 2^32 become 2^31. We use NMANTISSA for
00488 this term.  The exponential distribution is one sided so we use the
00489 full 32 bits.  We use EMANTISSA for this term.
00490 
00491 It appears that I'm slightly slower than the code in the article, this
00492 is partially due to a better generator of random integers than they
00493 use. But might also be that the case of rapid return was optimized by
00494 inlining the relevant code with a #define. As the basic Mersenne
00495 Twister is only 25% faster than this code I suspect that the main
00496 reason is just the use of the Mersenne Twister and not the inlining,
00497 so I'm not going to try and optimize further.
00498 */
00499 
00500 static void
00501 create_ziggurat_tables (void)
00502 {
00503   int i;
00504   double x, x1;
00505 
00506   /* Ziggurat tables for the normal distribution */
00507   x1 = ZIGGURAT_NOR_R;
00508   wi[255] = x1 / NMANTISSA;
00509   fi[255] = exp (-0.5 * x1 * x1);
00510 
00511   /* Index zero is special for tail strip, where Marsaglia and Tsang
00512    * defines this as
00513    * k_0 = 2^31 * r * f(r) / v, w_0 = 0.5^31 * v / f(r), f_0 = 1,
00514    * where v is the area of each strip of the ziggurat.
00515    */
00516   ki[0] = (ZIGINT) (x1 * fi[255] / NOR_SECTION_AREA * NMANTISSA);
00517   wi[0] = NOR_SECTION_AREA / fi[255] / NMANTISSA;
00518   fi[0] = 1.;
00519 
00520   for (i = 254; i > 0; i--)
00521     {
00522       /* New x is given by x = f^{-1}(v/x_{i+1} + f(x_{i+1})), thus
00523        * need inverse operator of y = exp(-0.5*x*x) -> x = sqrt(-2*ln(y))
00524        */
00525       x = sqrt(-2. * log(NOR_SECTION_AREA / x1 + fi[i+1]));
00526       ki[i+1] = (ZIGINT)(x / x1 * NMANTISSA);
00527       wi[i] = x / NMANTISSA;
00528       fi[i] = exp (-0.5 * x * x);
00529       x1 = x;
00530     }
00531 
00532   ki[1] = 0;
00533 
00534   /* Zigurrat tables for the exponential distribution */
00535   x1 = ZIGGURAT_EXP_R;
00536   we[255] = x1 / EMANTISSA;
00537   fe[255] = exp (-x1);
00538 
00539   /* Index zero is special for tail strip, where Marsaglia and Tsang
00540    * defines this as
00541    * k_0 = 2^32 * r * f(r) / v, w_0 = 0.5^32 * v / f(r), f_0 = 1,
00542    * where v is the area of each strip of the ziggurat.
00543    */
00544   ke[0] = (ZIGINT) (x1 * fe[255] / EXP_SECTION_AREA * EMANTISSA);
00545   we[0] = EXP_SECTION_AREA / fe[255] / EMANTISSA;
00546   fe[0] = 1.;
00547 
00548   for (i = 254; i > 0; i--)
00549     {
00550       /* New x is given by x = f^{-1}(v/x_{i+1} + f(x_{i+1})), thus
00551        * need inverse operator of y = exp(-x) -> x = -ln(y)
00552        */
00553       x = - log(EXP_SECTION_AREA / x1 + fe[i+1]);
00554       ke[i+1] = (ZIGINT)(x / x1 * EMANTISSA);
00555       we[i] = x / EMANTISSA;
00556       fe[i] = exp (-x);
00557       x1 = x;
00558     }
00559   ke[1] = 0;
00560 
00561   initt = 0;
00562 }
00563 
00564 /*
00565  * Here is the guts of the algorithm. As Marsaglia and Tsang state the
00566  * algorithm in their paper
00567  *
00568  * 1) Calculate a random signed integer j and let i be the index
00569  *     provided by the rightmost 8-bits of j
00570  * 2) Set x = j * w_i. If j < k_i return x
00571  * 3) If i = 0, then return x from the tail
00572  * 4) If [f(x_{i-1}) - f(x_i)] * U < f(x) - f(x_i), return x
00573  * 5) goto step 1
00574  *
00575  * Where f is the functional form of the distribution, which for a normal
00576  * distribution is exp(-0.5*x*x)
00577  */
00578 
00579 double
00580 oct_randn (void)
00581 {
00582   if (initt)
00583     create_ziggurat_tables();
00584 
00585   while (1)
00586     {
00587       /* The following code is specialized for 32-bit mantissa.
00588        * Compared to the arbitrary mantissa code, there is a performance
00589        * gain for 32-bits:  PPC: 2%, MIPS: 8%, x86: 40%
00590        * There is a bigger performance gain compared to using a full
00591        * 53-bit mantissa:  PPC: 60%, MIPS: 65%, x86: 240%
00592        * Of course, different compilers and operating systems may
00593        * have something to do with this.
00594        */
00595 #if !defined(ALLBITS)
00596 # if HAVE_X86_32
00597       /* 53-bit mantissa, 1-bit sign, x86 32-bit architecture */
00598       double x;
00599       int si,idx;
00600       register uint32_t lo, hi;
00601       int64_t rabs;
00602       uint32_t *p = (uint32_t *)&rabs;
00603       lo = randi32();
00604       idx = lo&0xFF;
00605       hi = randi32();
00606       si = hi&UMASK;
00607       p[0] = lo;
00608       p[1] = hi&0x1FFFFF;
00609       x = ( si ? -rabs : rabs ) * wi[idx];
00610 # else /* !HAVE_X86_32 */
00611       /* arbitrary mantissa (selected by NRANDI, with 1 bit for sign) */
00612       const uint64_t r = NRANDI;
00613       const int64_t rabs=r>>1;
00614       const int idx = (int)(rabs&0xFF);
00615       const double x = ( r&1 ? -rabs : rabs) * wi[idx];
00616 # endif /* !HAVE_X86_32 */
00617       if (rabs < (int64_t)ki[idx])
00618 #else /* ALLBITS */
00619       /* 32-bit mantissa */
00620       const uint32_t r = randi32();
00621       const uint32_t rabs = r&LMASK;
00622       const int idx = (int)(r&0xFF);
00623       const double x = ((int32_t)r) * wi[idx];
00624       if (rabs < ki[idx])
00625 #endif /* ALLBITS */
00626         return x;        /* 99.3% of the time we return here 1st try */
00627       else if (idx == 0)
00628         {
00629           /* As stated in Marsaglia and Tsang
00630            *
00631            * For the normal tail, the method of Marsaglia[5] provides:
00632            * generate x = -ln(U_1)/r, y = -ln(U_2), until y+y > x*x,
00633            * then return r+x. Except that r+x is always in the positive
00634            * tail!!!! Any thing random might be used to determine the
00635            * sign, but as we already have r we might as well use it
00636            *
00637            * [PAK] but not the bottom 8 bits, since they are all 0 here!
00638            */
00639           double xx, yy;
00640           do
00641             {
00642               xx = - ZIGGURAT_NOR_INV_R * log (RANDU);
00643               yy = - log (RANDU);
00644             }
00645           while ( yy+yy <= xx*xx);
00646           return (rabs&0x100 ? -ZIGGURAT_NOR_R-xx : ZIGGURAT_NOR_R+xx);
00647         }
00648       else if ((fi[idx-1] - fi[idx]) * RANDU + fi[idx] < exp(-0.5*x*x))
00649         return x;
00650     }
00651 }
00652 
00653 double
00654 oct_rande (void)
00655 {
00656   if (initt)
00657     create_ziggurat_tables();
00658 
00659   while (1)
00660     {
00661       ZIGINT ri = ERANDI;
00662       const int idx = (int)(ri & 0xFF);
00663       const double x = ri * we[idx];
00664       if (ri < ke[idx])
00665         return x;               // 98.9% of the time we return here 1st try
00666       else if (idx == 0)
00667         {
00668           /* As stated in Marsaglia and Tsang
00669            *
00670            * For the exponential tail, the method of Marsaglia[5] provides:
00671            * x = r - ln(U);
00672            */
00673           return ZIGGURAT_EXP_R - log(RANDU);
00674         }
00675       else if ((fe[idx-1] - fe[idx]) * RANDU + fe[idx] < exp(-x))
00676         return x;
00677     }
00678 }
00679 
00680 /* Array generators */
00681 void
00682 oct_fill_randu (octave_idx_type n, double *p)
00683 {
00684   octave_idx_type i;
00685   for (i = 0; i < n; i++)
00686     p[i] = oct_randu();
00687 }
00688 
00689 void
00690 oct_fill_randn (octave_idx_type n, double *p)
00691 {
00692   octave_idx_type i;
00693   for (i = 0; i < n; i++)
00694     p[i] = oct_randn();
00695 }
00696 
00697 void
00698 oct_fill_rande (octave_idx_type n, double *p)
00699 {
00700   octave_idx_type i;
00701   for (i = 0; i < n; i++)
00702     p[i] = oct_rande();
00703 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines