random.c 750 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include <stdlib.h>
  2. /*
  3. * This generator is a combination of three linear congruential generators
  4. * with periods or 2^15-405, 2^15-1041 and 2^15-1111. It has a period that
  5. * is the product of these three numbers.
  6. */
  7. static long int seed1 = 1;
  8. static long int seed2 = 1;
  9. static long int seed3 = 1;
  10. #define CRANK(a,b,c,m,s) \
  11. q = s/a; \
  12. s = b*(s-a*q) - c*q; \
  13. if(s<0) s+=m;
  14. long int random()
  15. {
  16. register long int q;
  17. CRANK(206, 157, 31, 32363, seed1);
  18. CRANK(217, 146, 45, 31727, seed2);
  19. CRANK(222, 142, 133, 31657, seed3);
  20. return seed1 ^ seed2 ^ seed3;
  21. }
  22. void srandom(unsigned int seed)
  23. {
  24. seed &= RAND_MAX;
  25. seed1 = seed % 32362 + 1;
  26. seed2 = seed % 31726 + 1;
  27. seed3 = seed % 31656 + 1;
  28. }
  29. __asm__(".weak srand; srand = srandom");