random.c 708 B

123456789101112131415161718192021222324252627282930313233343536
  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. }