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.

94 lines
2.5 KiB

  1. #ifndef __INC_LIB8TION_RANDOM_H
  2. #define __INC_LIB8TION_RANDOM_H
  3. ///@ingroup lib8tion
  4. ///@defgroup Random Fast random number generators
  5. /// Fast 8- and 16- bit unsigned random numbers.
  6. /// Significantly faster than Arduino random(), but
  7. /// also somewhat less random. You can add entropy.
  8. ///@{
  9. // X(n+1) = (2053 * X(n)) + 13849)
  10. #define FASTLED_RAND16_2053 ((uint16_t)(2053))
  11. #define FASTLED_RAND16_13849 ((uint16_t)(13849))
  12. /// random number seed
  13. extern uint16_t rand16seed;// = RAND16_SEED;
  14. /// Generate an 8-bit random number
  15. LIB8STATIC uint8_t random8(void)
  16. {
  17. rand16seed = (rand16seed * FASTLED_RAND16_2053) + FASTLED_RAND16_13849;
  18. // return the sum of the high and low bytes, for better
  19. // mixing and non-sequential correlation
  20. return (uint8_t)(((uint8_t)(rand16seed & 0xFF)) +
  21. ((uint8_t)(rand16seed >> 8)));
  22. }
  23. /// Generate a 16 bit random number
  24. LIB8STATIC uint16_t random16(void)
  25. {
  26. rand16seed = (rand16seed * FASTLED_RAND16_2053) + FASTLED_RAND16_13849;
  27. return rand16seed;
  28. }
  29. /// Generate an 8-bit random number between 0 and lim
  30. /// @param lim the upper bound for the result
  31. LIB8STATIC uint8_t random8_max(uint8_t lim)
  32. {
  33. uint8_t r = random8();
  34. r = (r*lim) >> 8;
  35. return r;
  36. }
  37. /// Generate an 8-bit random number in the given range
  38. /// @param min the lower bound for the random number
  39. /// @param lim the upper bound for the random number
  40. LIB8STATIC uint8_t random8_min_max(uint8_t min, uint8_t lim)
  41. {
  42. uint8_t delta = lim - min;
  43. uint8_t r = random8_max(delta) + min;
  44. return r;
  45. }
  46. /// Generate an 16-bit random number between 0 and lim
  47. /// @param lim the upper bound for the result
  48. LIB8STATIC uint16_t random16_max(uint16_t lim)
  49. {
  50. uint16_t r = random16();
  51. uint32_t p = (uint32_t)lim * (uint32_t)r;
  52. r = p >> 16;
  53. return r;
  54. }
  55. /// Generate an 16-bit random number in the given range
  56. /// @param min the lower bound for the random number
  57. /// @param lim the upper bound for the random number
  58. LIB8STATIC uint16_t random16_min_max( uint16_t min, uint16_t lim)
  59. {
  60. uint16_t delta = lim - min;
  61. uint16_t r = random16_max(delta) + min;
  62. return r;
  63. }
  64. /// Set the 16-bit seed used for the random number generator
  65. LIB8STATIC void random16_set_seed(uint16_t seed)
  66. {
  67. rand16seed = seed;
  68. }
  69. /// Get the current seed value for the random number generator
  70. LIB8STATIC uint16_t random16_get_seed(void)
  71. {
  72. return rand16seed;
  73. }
  74. /// Add entropy into the random number generator
  75. LIB8STATIC void random16_add_entropy(uint16_t entropy)
  76. {
  77. rand16seed += entropy;
  78. }
  79. ///@}
  80. #endif