You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 line
804B

  1. #include <stdint.h>
  2. static uint32_t seed;
  3. void randomSeed(uint32_t newseed)
  4. {
  5. if (newseed > 0) seed = newseed;
  6. }
  7. void srandom(uint32_t newseed)
  8. {
  9. seed = newseed;
  10. }
  11. int32_t random(void)
  12. {
  13. int32_t hi, lo, x;
  14. // the algorithm used in avr-libc 1.6.4
  15. x = seed;
  16. if (x == 0) x = 123459876;
  17. hi = x / 127773;
  18. lo = x % 127773;
  19. x = 16807 * lo - 2836 * hi;
  20. if (x < 0) x += 0x7FFFFFFF;
  21. seed = x;
  22. return x;
  23. }
  24. uint32_t random(uint32_t howbig)
  25. {
  26. if (howbig == 0) return 0;
  27. return random() % howbig;
  28. }
  29. int32_t random(int32_t howsmall, int32_t howbig)
  30. {
  31. if (howsmall >= howbig) return howsmall;
  32. int32_t diff = howbig - howsmall;
  33. return random(diff) + howsmall;
  34. }
  35. unsigned int makeWord(unsigned int w) { return w; }
  36. unsigned int makeWord(unsigned char h, unsigned char l) { return (h << 8) | l; }