FastLED 3.9.15
Loading...
Searching...
No Matches
random8.h
Go to the documentation of this file.
1#pragma once
2
3#ifndef __INC_LIB8TION_RANDOM_H
4#define __INC_LIB8TION_RANDOM_H
5
6#include "platforms/is_platform.h"
7
8#if defined(FL_IS_AVR)
9#include "platforms/avr/is_avr.h" // ok platform headers // IWYU pragma: keep
10#endif
11
12#include "fl/stl/stdint.h"
13
14#include "fl/math/lib8static.h"
15#include "fl/stl/noexcept.h"
16
20
23
34
36#define FASTLED_RAND16_2053 ((fl::u16)(2053))
38#define FASTLED_RAND16_13849 ((fl::u16)(13849))
39
40#ifdef FL_IS_AVR_ATTINY
42#define APPLY_FASTLED_RAND16_2053(x) (x << 11) + (x << 2) + x
43#else
45#define APPLY_FASTLED_RAND16_2053(x) (x * FASTLED_RAND16_2053)
46#endif
47
49extern fl::u16 rand16seed; // = RAND16_SEED;
50
55 // return the sum of the high and low bytes, for better
56 // mixing and non-sequential correlation
57 return (fl::u8)(((fl::u8)(rand16seed & 0xFF)) +
58 ((fl::u8)(rand16seed >> 8)));
59}
60
67
71 fl::u8 r = random8();
72 r = (r * lim) >> 8;
73 return r;
74}
75
80 fl::u8 delta = lim - min;
81 fl::u8 r = random8(delta) + min;
82 return r;
83}
84
87LIB8STATIC fl::u16 random16(fl::u16 lim) FL_NOEXCEPT {
88 fl::u16 r = random16();
89 fl::u32 p = (fl::u32)lim * (fl::u32)r;
90 r = p >> 16;
91 return r;
92}
93
97LIB8STATIC fl::u16 random16(fl::u16 min, fl::u16 lim) FL_NOEXCEPT {
98 fl::u16 delta = lim - min;
99 fl::u16 r = random16(delta) + min;
100 return r;
101}
102
105
108
111 rand16seed += entropy;
112}
113
116
117#endif
FL_DISABLE_WARNING_PUSH U constexpr common_type_t< T, U > min(T a, U b) FL_NOEXCEPT
Memory functions are available in fl:: namespace via fl/stl/cstring.h Using declarations cannot work ...
Definition math.h:71
Defines static inlining macros for lib8tion functions.
LIB8STATIC void random16_add_entropy(fl::u16 entropy) FL_NOEXCEPT
Add entropy into the random number generator.
Definition random8.h:110
fl::u16 rand16seed
Seed for the random number generator functions.
LIB8STATIC void random16_set_seed(fl::u16 seed) FL_NOEXCEPT
Set the 16-bit seed used for the random number generator.
Definition random8.h:104
LIB8STATIC fl::u16 random16_get_seed() FL_NOEXCEPT
Get the current seed value for the random number generator.
Definition random8.h:107
#define APPLY_FASTLED_RAND16_2053(x)
Multiplies a value by the pseudo-random multiplier.
Definition random8.h:45
#define FASTLED_RAND16_13849
Increment value for pseudo-random number generation.
Definition random8.h:38
LIB8STATIC fl::u16 random16() FL_NOEXCEPT
Generate a 16-bit random number.
Definition random8.h:63
LIB8STATIC fl::u8 random8() FL_NOEXCEPT
Generate an 8-bit random number.
Definition random8.h:53
#define LIB8STATIC
Define a LIB8TION member function as static inline with an "unused" attribute.
Definition lib8static.h:12
unsigned char u8
Definition stdint.h:131
#define FL_NOEXCEPT