random.h 871 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * $RANDOM support.
  4. *
  5. * Copyright (C) 2009 Denys Vlasenko
  6. *
  7. * Licensed under GPLv2, see file LICENSE in this source tree.
  8. */
  9. #ifndef SHELL_RANDOM_H
  10. #define SHELL_RANDOM_H 1
  11. PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN
  12. typedef struct random_t {
  13. /* State of random number generators: */
  14. /* Galois LFSR (fast but weak) */
  15. int32_t galois_LFSR; /* must be signed! */
  16. /* LCG (fast but weak) */
  17. uint32_t LCG;
  18. /* 64-bit xorshift (fast, moderate strength) */
  19. uint32_t xs64_x;
  20. uint32_t xs64_y;
  21. } random_t;
  22. #define UNINITED_RANDOM_T(rnd) \
  23. ((rnd)->galois_LFSR == 0)
  24. #define INIT_RANDOM_T(rnd, nonzero, v) \
  25. ((rnd)->galois_LFSR = (rnd)->xs64_x = (nonzero), (rnd)->LCG = (rnd)->xs64_y = (v))
  26. #define CLEAR_RANDOM_T(rnd) \
  27. ((rnd)->galois_LFSR = 0)
  28. uint32_t next_random(random_t *rnd) FAST_FUNC;
  29. POP_SAVED_FUNCTION_VISIBILITY
  30. #endif