odyssey/third_party/machinarium/sources/lrand48.c

50 lines
965 B
C
Raw Normal View History

2019-01-30 09:30:31 +00:00
/*
* machinarium.
*
* cooperative multitasking engine.
*/
2019-01-30 09:30:31 +00:00
#include <assert.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
2019-01-30 09:30:31 +00:00
#include <time.h>
#include <unistd.h>
2019-01-30 09:30:31 +00:00
__thread unsigned short prng_seed[3];
__thread unsigned short *prng_state = NULL;
long int pg_lrand48(unsigned short *_rand48_seed);
void pg_srand48(long seed, unsigned short *_rand48_seed);
2019-01-30 09:30:31 +00:00
void mm_lrand48_seed(void)
2019-01-30 09:30:31 +00:00
{
struct timeval tv;
gettimeofday(&tv, NULL);
long int rand_seed_2 = 0;
long int rand_seed;
rand_seed = getpid() ^ getuid() ^ tv.tv_sec ^ tv.tv_usec;
int fd;
fd = open("/dev/urandom", O_RDONLY);
if (fd == -1)
fd = open("/dev/random", O_RDONLY);
if (fd != -1) {
int rc = read(fd, &rand_seed_2, sizeof(rand_seed_2));
(void)rc;
close(fd);
}
rand_seed ^= rand_seed_2;
pg_srand48(rand_seed, prng_seed);
prng_state = prng_seed;
2019-01-30 09:30:31 +00:00
}
long int machine_lrand48(void)
2019-01-30 09:30:31 +00:00
{
assert(prng_state);
return pg_lrand48(prng_state);
}