157 #if defined (HAVE_CONFIG_H)
173 #if ! defined (USE_X86_32)
174 # if defined (i386) || defined (HAVE_X86_32)
175 # define USE_X86_32 1
177 # define USE_X86_32 0
186 #define MATRIX_A 0x9908b0dfUL
187 #define UMASK 0x80000000UL
188 #define LMASK 0x7fffffffUL
189 #define MIXBITS(u,v) ( ((u) & UMASK) | ((v) & LMASK) )
190 #define TWIST(u,v) ((MIXBITS(u,v) >> 1) ^ ((v)&1UL ? MATRIX_A : 0UL))
192 static uint32_t *next;
193 static uint32_t state[
MT_N];
195 static int initf = 0;
196 static int initt = 1;
197 static int inittf = 1;
204 state[0] = s & 0xffffffffUL;
205 for (j = 1; j <
MT_N; j++)
207 state[j] = (1812433253UL * (state[j-1] ^ (state[j-1] >> 30)) + j);
212 state[j] &= 0xffffffffUL;
228 k = (
MT_N > key_length ?
MT_N : key_length);
231 state[i] = (state[i] ^ ((state[i-1] ^ (state[i-1] >> 30)) * 1664525UL))
233 state[i] &= 0xffffffffUL;
238 state[0] = state[
MT_N-1];
244 for (k =
MT_N - 1; k; k--)
246 state[i] = (state[i] ^ ((state[i-1] ^ (state[i-1] >> 30)) * 1566083941UL))
248 state[i] &= 0xffffffffUL;
252 state[0] = state[
MT_N-1];
257 state[0] = 0x80000000UL;
265 uint32_t entropy[
MT_N];
274 entropy[
n++] = now.unix_time ();
278 entropy[
n++] = clock ();
282 entropy[
n++] = now.usec ();
297 std::random_device rd;
298 std::uniform_int_distribution<uint32_t> dist;
302 entropy[
n++] = dist (rd);
304 catch (
const std::exception&)
317 std::copy_n (save,
MT_N, state);
319 next = state + (
MT_N - left + 1);
325 std::copy_n (state,
MT_N, save);
348 for (j =
MT_M; --j; p++)
366 y ^= (y << 7) & 0x9d2c5680UL;
367 y ^= (y << 15) & 0xefc60000UL;
368 return (y ^ (y >> 18));
374 #define randi32 randmt
379 const uint32_t lo =
randi32 ();
380 const uint32_t hi =
randi32 () & 0x1FFFFF;
381 #if defined (HAVE_X86_32)
383 uint32_t *p = (uint32_t *)&u;
388 return ((
static_cast<uint64_t
> (hi) << 32) | lo);
395 const uint32_t lo =
randi32 ();
396 const uint32_t hi =
randi32 () & 0x3FFFFF;
397 #if defined (HAVE_X86_32)
399 uint32_t *p =
static_cast<uint32_t *
> (&u);
404 return ((
static_cast<uint64_t
> (hi) << 32) | lo);
416 i =
randi32 () &
static_cast<uint32_t
> (0xFFFFFF);
420 return i * (1.0f / 16777216.0f);
434 while (a == 0 && b == 0);
436 return (a*67108864.0 + b) * (1.0/9007199254740992.0);
457 #define ZIGGURAT_TABLE_SIZE 256
459 #define ZIGGURAT_NOR_R 3.6541528853610088
460 #define ZIGGURAT_NOR_INV_R 0.27366123732975828
461 #define NOR_SECTION_AREA 0.00492867323399
463 #define ZIGGURAT_EXP_R 7.69711747013104972
464 #define ZIGGURAT_EXP_INV_R 0.129918765548341586
465 #define EXP_SECTION_AREA 0.0039496598225815571993
467 #define ZIGINT uint64_t
468 #define EMANTISSA 9007199254740992.0
469 #define ERANDI randi53()
470 #define NMANTISSA EMANTISSA
471 #define NRANDI randi54()
472 #define RANDU randu53()
526 fi[255] = exp (-0.5 * x1 * x1);
537 for (i = 254; i > 0; i--)
545 fi[i] = exp (-0.5 *
x *
x);
565 for (i = 254; i > 0; i--)
614 # if defined (HAVE_X86_32)
620 uint32_t *p = (uint32_t *)&rabs;
626 p[1] = hi & 0x1FFFFF;
627 x = ( si ? -rabs : rabs ) * wi[idx];
631 const int64_t rabs =
r >> 1;
632 const int idx =
static_cast<int> (rabs & 0xFF);
633 const double x = ( (
r & 1) ? -rabs : rabs) * wi[idx];
635 if (rabs <
static_cast<int64_t
> (ki[idx]))
653 yy = - std::log (
RANDU);
655 while ( yy+yy <= xx*xx);
658 else if ((fi[idx-1] - fi[idx]) *
RANDU + fi[idx] < exp (-0.5*
x*
x))
673 const int idx =
static_cast<int> (ri & 0xFF);
674 const double x = ri * we[idx];
686 else if ((fe[idx-1] - fe[idx]) *
RANDU + fe[idx] < exp (-
x))
713 #define ZIGINT uint32_t
714 #define EMANTISSA 4294967296.0
715 #define ERANDI randi32()
716 #define NMANTISSA 2147483648.0
717 #define NRANDI randi32()
718 #define RANDU randu24()
726 create_ziggurat_float_tables ()
734 ffi[255] = exp (-0.5 * x1 * x1);
745 for (i = 254; i > 0; i--)
753 ffi[i] = exp (-0.5 *
x *
x);
762 ffe[255] = exp (-x1);
773 for (i = 254; i > 0; i--)
809 create_ziggurat_float_tables ();
815 const uint32_t rabs =
r &
LMASK;
816 const int idx =
static_cast<int> (
r & 0xFF);
817 const float x =
static_cast<int32_t
> (
r) * fwi[idx];
836 yy = - std::log (
RANDU);
838 while ( yy+yy <= xx*xx);
841 else if ((ffi[idx-1] - ffi[idx]) *
RANDU + ffi[idx] < exp (-0.5*
x*
x))
851 create_ziggurat_float_tables ();
856 const int idx =
static_cast<int> (ri & 0xFF);
857 const float x = ri * fwe[idx];
869 else if ((ffe[idx-1] - ffe[idx]) *
RANDU + ffe[idx] < exp (-
x))
889 OCTAVE_END_NAMESPACE(
octave)
charNDArray min(char d, const charNDArray &m)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
F77_RET_T const F77_DBLE * x
void get_mersenne_twister_state(uint32_t *save)
double rand_uniform< double >()
void set_mersenne_twister_state(const uint32_t *save)
#define ZIGGURAT_TABLE_SIZE
void rand_normal(octave_idx_type n, double *p)
double rand_normal< double >()
void rand_uniform(octave_idx_type n, float *p)
void init_mersenne_twister(const uint32_t s)
float rand_normal< float >()
float rand_exponential< float >()
void create_ziggurat_tables()
#define ZIGGURAT_NOR_INV_R
double rand_exponential< double >()
void rand_exponential(octave_idx_type n, double *p)
float rand_uniform< float >()