Advertisement
Omnikron13

Light Implementation of Multiply-With-Carry PRNG v2

Mar 5th, 2012
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Lightweight adaptation of the example multiply with carry PRNG
  3.  * code from wikipedia
  4.  * Version: 2
  5.  */
  6.  
  7. #include <stdint.h>
  8.  
  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. uint32_t rand_cmwc(void)
  19. {   uint32_t x;
  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 = MWC_R - x);
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement