FastLED  3.1
random8.h
1 #ifndef __INC_LIB8TION_RANDOM_H
2 #define __INC_LIB8TION_RANDOM_H
3 
10 
11 // X(n+1) = (2053 * X(n)) + 13849)
12 #define FASTLED_RAND16_2053 ((uint16_t)(2053))
13 #define FASTLED_RAND16_13849 ((uint16_t)(13849))
14 
16 extern uint16_t rand16seed;// = RAND16_SEED;
17 
19 LIB8STATIC uint8_t random8()
20 {
21  rand16seed = (rand16seed * FASTLED_RAND16_2053) + FASTLED_RAND16_13849;
22  // return the sum of the high and low bytes, for better
23  // mixing and non-sequential correlation
24  return (uint8_t)(((uint8_t)(rand16seed & 0xFF)) +
25  ((uint8_t)(rand16seed >> 8)));
26 }
27 
29 LIB8STATIC uint16_t random16()
30 {
31  rand16seed = (rand16seed * FASTLED_RAND16_2053) + FASTLED_RAND16_13849;
32  return rand16seed;
33 }
34 
37 LIB8STATIC uint8_t random8(uint8_t lim)
38 {
39  uint8_t r = random8();
40  r = (r*lim) >> 8;
41  return r;
42 }
43 
47 LIB8STATIC uint8_t random8(uint8_t min, uint8_t lim)
48 {
49  uint8_t delta = lim - min;
50  uint8_t r = random8(delta) + min;
51  return r;
52 }
53 
56 LIB8STATIC uint16_t random16( uint16_t lim)
57 {
58  uint16_t r = random16();
59  uint32_t p = (uint32_t)lim * (uint32_t)r;
60  r = p >> 16;
61  return r;
62 }
63 
67 LIB8STATIC uint16_t random16( uint16_t min, uint16_t lim)
68 {
69  uint16_t delta = lim - min;
70  uint16_t r = random16( delta) + min;
71  return r;
72 }
73 
75 LIB8STATIC void random16_set_seed( uint16_t seed)
76 {
77  rand16seed = seed;
78 }
79 
81 LIB8STATIC uint16_t random16_get_seed()
82 {
83  return rand16seed;
84 }
85 
87 LIB8STATIC void random16_add_entropy( uint16_t entropy)
88 {
89  rand16seed += entropy;
90 }
91 
93 
94 #endif
uint16_t rand16seed
random number seed
Definition: lib8tion.cpp:8
LIB8STATIC void random16_set_seed(uint16_t seed)
Set the 16-bit seed used for the random number generator.
Definition: random8.h:75
LIB8STATIC uint8_t random8()
Generate an 8-bit random number.
Definition: random8.h:19
LIB8STATIC void random16_add_entropy(uint16_t entropy)
Add entropy into the random number generator.
Definition: random8.h:87
LIB8STATIC uint16_t random16_get_seed()
Get the current seed value for the random number generator.
Definition: random8.h:81
LIB8STATIC uint16_t random16()
Generate a 16 bit random number.
Definition: random8.h:29