2009-03-02 22:16:30 -05:00
|
|
|
/*
|
|
|
|
random numbers
|
|
|
|
*/
|
2019-08-09 12:00:17 -04:00
|
|
|
|
2009-03-02 22:16:30 -05:00
|
|
|
#include <math.h>
|
2019-08-09 16:31:21 -04:00
|
|
|
#include <stdint.h>
|
2019-08-09 12:00:17 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2009-03-02 22:16:30 -05:00
|
|
|
#include "dtypes.h"
|
2009-08-28 20:54:51 -04:00
|
|
|
#include "ieee754.h"
|
2009-03-02 22:16:30 -05:00
|
|
|
#include "utils.h"
|
|
|
|
#include "random.h"
|
|
|
|
#include "timefuncs.h"
|
|
|
|
|
2019-08-09 11:30:02 -04:00
|
|
|
#include "mt19937ar.h"
|
2009-03-02 22:16:30 -05:00
|
|
|
|
|
|
|
double rand_double()
|
|
|
|
{
|
|
|
|
union ieee754_double d;
|
|
|
|
|
2010-08-04 15:03:19 -04:00
|
|
|
d.ieee.mantissa0 = genrand_int32();
|
|
|
|
d.ieee.mantissa1 = genrand_int32();
|
2009-03-02 22:16:30 -05:00
|
|
|
d.ieee.negative = 0;
|
2019-08-09 07:02:02 -04:00
|
|
|
d.ieee.exponent = IEEE754_DOUBLE_BIAS + 0; /* 2^0 */
|
2009-03-02 22:16:30 -05:00
|
|
|
return d.d - 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
float rand_float()
|
|
|
|
{
|
|
|
|
union ieee754_float f;
|
|
|
|
|
2010-08-04 15:03:19 -04:00
|
|
|
f.ieee.mantissa = genrand_int32();
|
2009-03-02 22:16:30 -05:00
|
|
|
f.ieee.negative = 0;
|
2019-08-09 07:02:02 -04:00
|
|
|
f.ieee.exponent = IEEE754_FLOAT_BIAS + 0; /* 2^0 */
|
2009-03-02 22:16:30 -05:00
|
|
|
return f.f - 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
double randn()
|
|
|
|
{
|
|
|
|
double s, vre, vim, ure, uim;
|
|
|
|
static double next = -42;
|
|
|
|
|
|
|
|
if (next != -42) {
|
|
|
|
s = next;
|
|
|
|
next = -42;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
ure = rand_double();
|
|
|
|
uim = rand_double();
|
2019-08-09 07:02:02 -04:00
|
|
|
vre = 2 * ure - 1;
|
|
|
|
vim = 2 * uim - 1;
|
|
|
|
s = vre * vre + vim * vim;
|
2009-03-02 22:16:30 -05:00
|
|
|
} while (s >= 1);
|
2019-08-09 07:02:02 -04:00
|
|
|
s = sqrt(-2 * log(s) / s);
|
2009-03-02 22:16:30 -05:00
|
|
|
next = s * vre;
|
|
|
|
return s * vim;
|
|
|
|
}
|
|
|
|
|
|
|
|
void randomize()
|
|
|
|
{
|
2019-08-09 14:00:03 -04:00
|
|
|
uint64_t tm = i64time();
|
2019-08-09 07:02:02 -04:00
|
|
|
init_by_array((uint32_t *)&tm, 2);
|
2009-03-02 22:16:30 -05:00
|
|
|
}
|