/src/rt/isaac/rand.h

http://github.com/jruderman/rust · C Header · 56 lines · 24 code · 9 blank · 23 comment · 0 complexity · 028dc6b6831999740230b8a84500ba62 MD5 · raw file

  1. /*
  2. ------------------------------------------------------------------------------
  3. rand.h: definitions for a random number generator
  4. By Bob Jenkins, 1996, Public Domain
  5. MODIFIED:
  6. 960327: Creation (addition of randinit, really)
  7. 970719: use context, not global variables, for internal state
  8. 980324: renamed seed to flag
  9. 980605: recommend RANDSIZL=4 for noncryptography.
  10. 010626: note this is public domain
  11. ------------------------------------------------------------------------------
  12. */
  13. #ifndef STANDARD
  14. #include "standard.h"
  15. #endif
  16. #ifndef RAND
  17. #define RAND
  18. #define RANDSIZL (8) /* I recommend 8 for crypto, 4 for simulations */
  19. #define RANDSIZ (1<<RANDSIZL)
  20. /* context of random number generator */
  21. struct randctx
  22. {
  23. ub4 randcnt;
  24. ub4 randrsl[RANDSIZ];
  25. ub4 randmem[RANDSIZ];
  26. ub4 randa;
  27. ub4 randb;
  28. ub4 randc;
  29. };
  30. typedef struct randctx randctx;
  31. /*
  32. ------------------------------------------------------------------------------
  33. If (flag==TRUE), then use the contents of randrsl[0..RANDSIZ-1] as the seed.
  34. ------------------------------------------------------------------------------
  35. */
  36. void randinit(randctx *r, word flag);
  37. void isaac(randctx *r);
  38. /*
  39. ------------------------------------------------------------------------------
  40. Call isaac_rand(/o_ randctx *r _o/) to retrieve a single 32-bit random value
  41. ------------------------------------------------------------------------------
  42. */
  43. #define isaac_rand(r) \
  44. (!(r)->randcnt-- ? \
  45. (isaac(r), (r)->randcnt=RANDSIZ-1, (r)->randrsl[(r)->randcnt]) : \
  46. (r)->randrsl[(r)->randcnt])
  47. #endif /* RAND */