FastLED 3.9.12
Loading...
Searching...
No Matches
random8.h
Go to the documentation of this file.
1#ifndef __INC_LIB8TION_RANDOM_H
2#define __INC_LIB8TION_RANDOM_H
3
4#include <stdint.h>
5
7
11
14
25
27#define FASTLED_RAND16_2053 ((uint16_t)(2053))
29#define FASTLED_RAND16_13849 ((uint16_t)(13849))
30
31#if defined(LIB8_ATTINY)
33#define APPLY_FASTLED_RAND16_2053(x) (x << 11) + (x << 2) + x
34#else
36#define APPLY_FASTLED_RAND16_2053(x) (x * FASTLED_RAND16_2053)
37#endif
38
40extern uint16_t rand16seed; // = RAND16_SEED;
41
46 // return the sum of the high and low bytes, for better
47 // mixing and non-sequential correlation
48 return (uint8_t)(((uint8_t)(rand16seed & 0xFF)) +
49 ((uint8_t)(rand16seed >> 8)));
50}
51
58
61LIB8STATIC uint8_t random8(uint8_t lim) {
62 uint8_t r = random8();
63 r = (r * lim) >> 8;
64 return r;
65}
66
70LIB8STATIC uint8_t random8(uint8_t min, uint8_t lim) {
71 uint8_t delta = lim - min;
72 uint8_t r = random8(delta) + min;
73 return r;
74}
75
78LIB8STATIC uint16_t random16(uint16_t lim) {
79 uint16_t r = random16();
80 uint32_t p = (uint32_t)lim * (uint32_t)r;
81 r = p >> 16;
82 return r;
83}
84
88LIB8STATIC uint16_t random16(uint16_t min, uint16_t lim) {
89 uint16_t delta = lim - min;
90 uint16_t r = random16(delta) + min;
91 return r;
92}
93
95LIB8STATIC void random16_set_seed(uint16_t seed) { rand16seed = seed; }
96
99
101LIB8STATIC void random16_add_entropy(uint16_t entropy) {
102 rand16seed += entropy;
103}
104
107
108#endif
LIB8STATIC void random16_add_entropy(uint16_t entropy)
Add entropy into the random number generator.
Definition random8.h:101
LIB8STATIC uint16_t random16()
Generate a 16-bit random number.
Definition random8.h:54
LIB8STATIC uint8_t random8()
Generate an 8-bit random number.
Definition random8.h:44
uint16_t rand16seed
Seed for the random number generator functions.
Definition lib8tion.cpp:17
LIB8STATIC uint16_t random16_get_seed()
Get the current seed value for the random number generator.
Definition random8.h:98
#define APPLY_FASTLED_RAND16_2053(x)
Multiplies a value by the pseudo-random multiplier.
Definition random8.h:36
#define FASTLED_RAND16_13849
Increment value for pseudo-random number generation.
Definition random8.h:29
LIB8STATIC void random16_set_seed(uint16_t seed)
Set the 16-bit seed used for the random number generator.
Definition random8.h:95
#define LIB8STATIC
Define a LIB8TION member function as static inline with an "unused" attribute.
Definition lib8static.h:10
Defines static inlining macros for lib8tion functions.