laferrierejc

CMWC

Oct 14th, 2012
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6.  
  7. #define PHI 0x9e3779b9
  8.  
  9. using namespace std;
  10.  
  11. static uint32_t Q[4096], c = 362436;
  12.  
  13. class cmwc
  14. {
  15.  
  16. public:
  17.  
  18. void init_rand(uint32_t x)
  19. {
  20. int i;
  21.  
  22. Q[0] = x;
  23. Q[1] = x + PHI;
  24. Q[2] = x + PHI + PHI;
  25.  
  26. for (i = 3; i < 4096; i++)
  27. Q[i] = Q[i - 3] ^ Q[i - 2] ^ PHI ^ i;
  28. }
  29.  
  30. uint32_t rand_cmwc(void)
  31. {
  32. uint64_t t, a = 18782LL;
  33. static uint32_t i = 4095;
  34. uint32_t x, r = 0xfffffffe;
  35. i = (i + 1) & 4095;
  36. t = a * Q[i] + c;
  37. c = (t >> 32);
  38. x = t + c;
  39. if (x < c) {
  40. x++;
  41. c++;
  42. }
  43. return (Q[i] = r - x);
  44. }
  45.  
  46. uint32_t random_num;
  47.  
  48. };
  49.  
  50. int main()
  51. {
  52. cmwc rng1;
  53.  
  54. rng1.init_rand(time(NULL));
  55.  
  56. rng1.random_num = 1 + rng1.rand_cmwc()%6;
  57.  
  58. //int r = rng1.rand_cmwc();
  59.  
  60. cout << "die: " << rng1.random_num << endl;
  61.  
  62.  
  63. return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment