Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdint.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #define PHI 0x9e3779b9
- using namespace std;
- static uint32_t Q[4096], c = 362436;
- class cmwc
- {
- public:
- void init_rand(uint32_t x)
- {
- int i;
- Q[0] = x;
- Q[1] = x + PHI;
- Q[2] = x + PHI + PHI;
- for (i = 3; i < 4096; i++)
- Q[i] = Q[i - 3] ^ Q[i - 2] ^ PHI ^ i;
- }
- uint32_t rand_cmwc(void)
- {
- uint64_t t, a = 18782LL;
- static uint32_t i = 4095;
- uint32_t x, r = 0xfffffffe;
- i = (i + 1) & 4095;
- t = a * Q[i] + c;
- c = (t >> 32);
- x = t + c;
- if (x < c) {
- x++;
- c++;
- }
- return (Q[i] = r - x);
- }
- uint32_t random_num;
- };
- int main()
- {
- cmwc rng1;
- rng1.init_rand(time(NULL));
- rng1.random_num = 1 + rng1.rand_cmwc()%6;
- //int r = rng1.rand_cmwc();
- cout << "die: " << rng1.random_num << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment