Advertisement
Omnikron13

Light Implementation of Multiply-With-Carry PRNG

Mar 5th, 2012
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.42 KB | None | 0 0
  1. /*
  2.  * Lightweight adaptation of the example multiply with carry PRNG
  3.  * code from wikipedia
  4.  * Version: 1
  5.  */
  6.  
  7. #include <stdint.h>
  8.  
  9. #define PHI 0x9e3779b9
  10.  
  11. static uint32_t Q, c = 362436;
  12.  
  13. void init_rand(uint32_t x)
  14. {   Q = x;
  15. }
  16.  
  17. uint32_t rand_cmwc(void)
  18. {   uint64_t t, a = 18782LL;
  19.     uint32_t x, r = 0xfffffffe;
  20.     t = 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);
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement