View difference between Paste ID: xnSKHUgA and MvSfnin3
SHOW: | | - or go back to the newest paste.
1
/* 
2
 * Lightweight adaptation of the example multiply with carry PRNG
3
 * code from wikipedia
4-
 * Version: 1
4+
 * Version: 2
5
 */
6
7
#include <stdint.h>
8
9-
#define PHI 0x9e3779b9
9+
#define MWC_A 18782LL
10
#define MWC_R 0xfffffffe
11
12
static uint32_t Q, c = 362436;
13
14
void init_rand(uint32_t x)
15
{	Q = x;
16
}
17
18-
{	uint64_t t, a = 18782LL;
18+
19-
	uint32_t x, r = 0xfffffffe;
19+
{	uint32_t x;
20-
	t = a * Q + c;
20+
	uint64_t t = MWC_A * Q + c;
21
	c = (t >> 32);
22
	x = t + c;
23
	if(x < c)
24
	{	x++;
25
		c++;
26
	}
27-
	return (Q = r - x);
27+
	return (Q = MWC_R - x);
28
}