SHARE
TWEET

16 bit xor shift rng

a guest Dec 5th, 2014 26 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // XOR SHIFT 16 bit RNG (Modified version of George Marsaglia's xorshift)
  2. // http://www.arklyffe.com/main/2010/08/29/xorshift-pseudorandom-number-generator/
  3. //
  4. //
  5. /* 60 Values that work for A,B,C in the 16-bit XORShift RNG.
  6. (1,  1, 14)     (1,  1, 15)     (1, 5, 2 )      (1,  7, 4 )     (1,  7, 11)     (1, 11, 3 )
  7. (1, 15, 6 )     (1, 15, 7 )     (2, 5, 1 )      (2,  5, 13)     (2,  5, 15)     (2,  7, 13)
  8. (2,  7, 15)     (3,  1, 12)     (3, 1, 15)      (3,  5, 11)     (3, 11, 1 )     (3, 11, 11)
  9. (3, 13, 9 )     (4,  3, 7 )     (4, 7, 1 )      (4, 11, 11)     (5,  7, 14)     (5,  9, 8 )
  10. (5, 11, 6 )     (5, 11, 11)     (6, 7, 13)      (6, 11, 5 )     (6, 15, 1 )     (7,  1, 11)
  11. (7,  3, 4 )     (7,  9, 8 )     (7, 9, 13)      (7, 15, 1 )     (8,  9, 5 )     (8,  9, 7 )
  12. (9,  7, 13)     (9, 13, 3 )     (11, 1, 7)      (11, 3, 13)     (11, 5, 3 )     (11, 7, 1 )
  13. (11,11, 3 )     (11,11, 4 )     (11,11, 5)      (12, 1, 3 )     (12, 3, 13)     (13, 3, 11)
  14. (13, 3, 12)     (13, 5, 2 )     (13, 7, 2)      (13, 7, 6 )     (13, 7, 9 )     (13, 9, 7 )
  15. (14, 1, 1 )     (14, 7, 5 )     (15, 1, 1)      (15, 1, 3 )     (15, 5, 2 )     (15, 7, 2 )
  16. */
  17.  
  18. int PR_Y = 1; //Seed, must not be zero.
  19. #define PR_A 13
  20. #define PR_B 9
  21. #define PR_C 7
  22.  
  23. function int PRandom(int min, int max) //Dont use negative values with this function
  24. {
  25.   int value;
  26.  
  27.   if(min == max) return min;
  28.   if(max < min)
  29.   {   //Swap values
  30.     value = max;
  31.     max = min;
  32.     min = value;
  33.   }
  34.  
  35.   PR_Y ^= (PR_Y << PR_A);
  36.   PR_Y ^= (PR_Y >> PR_B);
  37.   PR_Y ^= (PR_Y << PR_C);
  38.   value = PR_Y;
  39.  
  40.   if (value < 0) value = -value;
  41.   value = value % (max - min + 1);
  42.   value += min;
  43.  
  44.   return value;
  45. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top