shithub: sl

ref: a70379d7e4b822f532fb0a8ccdd1624a90b64a68
dir: /src/random.c/

View raw version
#include "sl.h"
#include "cvalues.h"
#include "mt19937-64.h"
#include "timefuncs.h"
#include "random.h"

static mt19937_64 ctx;

void
randomize(void)
{
	unsigned long long tm = sec_realtime() * 1000.0;
	init_by_array64(&ctx, &tm, 1);
}

BUILTIN("rand", rand)
{
	USED(args); USED(nargs);
#ifdef BITS64
	return fixnum(genrand64_int64(&ctx) >> 3);
#else
	return fixnum(genrand64_int64(&ctx) >> (32+3));
#endif
}

BUILTIN("rand-u32", rand_u32)
{
	USED(args); USED(nargs);
	return mk_u32(genrand64_int64(&ctx) >> 32);
}

BUILTIN("rand-u64", rand_u64)
{
	USED(args); USED(nargs);
	return mk_u64(genrand64_int64(&ctx));
}

BUILTIN("rand-double", rand_double)
{
	USED(args); USED(nargs);
	return mk_double(genrand64_real1(&ctx));
}

BUILTIN("rand-float", rand_float)
{
	USED(args); USED(nargs);
	return mk_float(genrand64_real1(&ctx));
}