PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

random8.h 2.6KB

3 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #if defined(LIB8_ATTINY)
  13. #define APPLY_FASTLED_RAND16_2053(x) (x << 11) + (x << 2) + x
  14. #else
  15. #define APPLY_FASTLED_RAND16_2053(x) (x * FASTLED_RAND16_2053)
  16. #endif
  17. /// random number seed
  18. extern uint16_t rand16seed;// = RAND16_SEED;
  19. /// Generate an 8-bit random number
  20. LIB8STATIC uint8_t random8()
  21. {
  22. rand16seed = APPLY_FASTLED_RAND16_2053(rand16seed) + FASTLED_RAND16_13849;
  23. // return the sum of the high and low bytes, for better
  24. // mixing and non-sequential correlation
  25. return (uint8_t)(((uint8_t)(rand16seed & 0xFF)) +
  26. ((uint8_t)(rand16seed >> 8)));
  27. }
  28. /// Generate a 16 bit random number
  29. LIB8STATIC uint16_t random16()
  30. {
  31. rand16seed = APPLY_FASTLED_RAND16_2053(rand16seed) + FASTLED_RAND16_13849;
  32. return rand16seed;
  33. }
  34. /// Generate an 8-bit random number between 0 and lim
  35. /// @param lim the upper bound for the result
  36. LIB8STATIC uint8_t random8(uint8_t lim)
  37. {
  38. uint8_t r = random8();
  39. r = (r*lim) >> 8;
  40. return r;
  41. }
  42. /// Generate an 8-bit random number in the given range
  43. /// @param min the lower bound for the random number
  44. /// @param lim the upper bound for the random number
  45. LIB8STATIC uint8_t random8(uint8_t min, uint8_t lim)
  46. {
  47. uint8_t delta = lim - min;
  48. uint8_t r = random8(delta) + min;
  49. return r;
  50. }
  51. /// Generate an 16-bit random number between 0 and lim
  52. /// @param lim the upper bound for the result
  53. LIB8STATIC uint16_t random16( uint16_t lim)
  54. {
  55. uint16_t r = random16();
  56. uint32_t p = (uint32_t)lim * (uint32_t)r;
  57. r = p >> 16;
  58. return r;
  59. }
  60. /// Generate an 16-bit random number in the given range
  61. /// @param min the lower bound for the random number
  62. /// @param lim the upper bound for the random number
  63. LIB8STATIC uint16_t random16( uint16_t min, uint16_t lim)
  64. {
  65. uint16_t delta = lim - min;
  66. uint16_t r = random16( delta) + min;
  67. return r;
  68. }
  69. /// Set the 16-bit seed used for the random number generator
  70. LIB8STATIC void random16_set_seed( uint16_t seed)
  71. {
  72. rand16seed = seed;
  73. }
  74. /// Get the current seed value for the random number generator
  75. LIB8STATIC uint16_t random16_get_seed()
  76. {
  77. return rand16seed;
  78. }
  79. /// Add entropy into the random number generator
  80. LIB8STATIC void random16_add_entropy( uint16_t entropy)
  81. {
  82. rand16seed += entropy;
  83. }
  84. ///@}
  85. #endif