~coco/imscript

100f09a48e58a6534be252289dde3e15362d7ce7 — enric meinhardt-llopis 21 days ago 9477a21
random.c: shuffle seed to avoid visible first runs
1 files changed, 15 insertions(+), 0 deletions(-)

M src/random.c
M src/random.c => src/random.c +15 -0
@@ 32,9 32,24 @@ static uint32_t lcg_knuth_rand(void)
	return lcg_knuth_seed >> 32;
}

// hacker's delight 32-bit shuffle
static uint32_t hdshuf(uint32_t x)
{
	x = (x & 0x0000FF00) << 8 | (x >> 8) & 0x0000FF00 | x & 0xFF0000FF;
	x = (x & 0x00F000F0) << 4 | (x >> 4) & 0x00F000F0 | x & 0xF00FF00F;
	x = (x & 0x0C0C0C0C) << 2 | (x >> 2) & 0x0C0C0C0C | x & 0xC3C3C3C3;
	x = (x & 0x22222222) << 1 | (x >> 1) & 0x22222222 | x & 0x99999999;
	return x;
}

static void xsrand(unsigned long int iseed)
{
	// shuffle the seed a bit to avoid visible runs in the common case
	// of consecutive seeds
	unsigned long int oseed = iseed;
	iseed += hdshuf(iseed);
	//fprintf(stderr, "shuffled seed %ld -> %ld\n", oseed, iseed);

	//uint64_t seed = iseed;
	//uint64_t f = 2097152 + 17; // cubic root of 2^63
	//uint64_t g = 549755813888 + 19;