Advertisement
yojimbos_law

ASH implementation of glibc's old rand() function.

Jun 18th, 2019
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. int seeded_rand(int seed, int low, int high, int position){
  2. int next;
  3. int but = 2**31;
  4. int bot = 2**16;
  5. int[int] s;
  6. int place = 0;
  7. void srand(int x){
  8. clear(s);
  9. //probably wants to be an unsigned int (i.e. in range [0,2**32-1]) based on php-src/ext/standard/rand.c lines 44-52
  10. s[0] = x;
  11. for i from 1 to 30{
  12. s[i] = (s[i-1] * 16807) % (but-1);
  13. }
  14. for i from 31 to 33{
  15. s[i] = s[i-31];
  16. }
  17. for i from 34 to (344+position+1){
  18. s[i] = (s[i-3] + s[i-31]) % (2*but);
  19. }
  20. }
  21.  
  22. int rand(int min, int max, int place){
  23. //LCG stuff:
  24. //from IBM and C and POSIX
  25. //next = (1103515245 * next + 12345);
  26. //next %= but;
  27. //next &= but-bot;
  28.  
  29. //from some other C thing
  30. //next = (1103515245 * next + 12345);
  31. //next %= but*2;
  32. //next &= but-bot;
  33.  
  34. //from MS Visual C
  35. //next = (214013 * next + 2531011);
  36. //next %= 2*but;
  37. //next &= but-bot;
  38.  
  39.  
  40. //glibc implementation
  41. next = s[344+place] >> 1;
  42.  
  43. return (min + ( max - min + 1 ) * ( next / ( (but -1) + 1.0))) ;
  44. //return (next);
  45. }
  46.  
  47. srand(seed);
  48. return rand(low, high, position);
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement