FastLED 3.9.15
Loading...
Searching...
No Matches
random.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/stl/int.h"
4#include "fl/math/random8.h"
5#include "fl/stl/noexcept.h"
6
7namespace fl {
8namespace math {
9
25class random {
26public:
27 typedef u32 result_type;
28
31
33 explicit random(u16 seed) FL_NOEXCEPT : mSeed(seed) {}
34
38
39 void set_seed(u16 seed) FL_NOEXCEPT { mSeed = seed; }
40 u16 get_seed() const FL_NOEXCEPT { return mSeed; }
41 void add_entropy(u16 entropy) FL_NOEXCEPT { mSeed += entropy; }
42
43 static constexpr result_type minimum() FL_NOEXCEPT { return 0; }
44 static constexpr result_type maximum() FL_NOEXCEPT { return 4294967295U; }
45
49
51 u16 random16(u16 n) FL_NOEXCEPT { return static_cast<u16>(generate_nolock(n)); }
52 u16 random16(u16 min, u16 max) FL_NOEXCEPT { return static_cast<u16>(generate_nolock(min, max)); }
53
54private:
55 u16 mSeed;
56
61
63 u32 high = next_random16_nolock();
64 u32 low = next_random16_nolock();
65 return (high << 16) | low;
66 }
67
71
73 if (n == 0) return 0;
74 u32 r = next_random32_nolock();
75 fl::u64 p = (fl::u64)n * (fl::u64)r;
76 return (u32)(p >> 32);
77 }
78
83
85 u16 r = next_random16_nolock();
86 return (u8)(((u8)(r & 0xFF)) + ((u8)(r >> 8)));
87 }
88
90 u8 r = random8_nolock();
91 r = (r * n) >> 8;
92 return r;
93 }
94
96 u8 delta = max - min;
97 return random8_nolock(delta) + min;
98 }
99
103};
104
105} // namespace math
106
107// Backward compatibility alias
109
115
116} // namespace fl
u32 next_random32_nolock() FL_NOEXCEPT
Definition random.h:62
result_type generate_nolock() FL_NOEXCEPT
Definition random.h:68
u8 random8(u8 n) FL_NOEXCEPT
Definition random.h:47
u16 get_seed() const FL_NOEXCEPT
Definition random.h:40
u8 random8_nolock(u8 min, u8 max) FL_NOEXCEPT
Definition random.h:95
random() FL_NOEXCEPT
Default constructor - uses current global random seed.
Definition random.h:30
u16 random16(u16 n) FL_NOEXCEPT
Definition random.h:51
u8 random8() FL_NOEXCEPT
Definition random.h:46
result_type operator()(result_type n) FL_NOEXCEPT
Definition random.h:36
u16 random16() FL_NOEXCEPT
Definition random.h:50
result_type operator()(result_type min, result_type max) FL_NOEXCEPT
Definition random.h:37
u16 random16_nolock() FL_NOEXCEPT
Definition random.h:100
u16 next_random16_nolock() FL_NOEXCEPT
Definition random.h:57
result_type generate_nolock(result_type min, result_type max) FL_NOEXCEPT
Definition random.h:79
void add_entropy(u16 entropy) FL_NOEXCEPT
Definition random.h:41
random(u16 seed) FL_NOEXCEPT
Constructor with explicit seed.
Definition random.h:33
void set_seed(u16 seed) FL_NOEXCEPT
Definition random.h:39
static constexpr result_type minimum() FL_NOEXCEPT
Definition random.h:43
u8 random8_nolock(u8 n) FL_NOEXCEPT
Definition random.h:89
result_type generate_nolock(result_type n) FL_NOEXCEPT
Definition random.h:72
static constexpr result_type maximum() FL_NOEXCEPT
Definition random.h:44
u16 random16(u16 min, u16 max) FL_NOEXCEPT
Definition random.h:52
result_type operator()() FL_NOEXCEPT
Definition random.h:35
u8 random8(u8 min, u8 max) FL_NOEXCEPT
Definition random.h:48
u8 random8_nolock() FL_NOEXCEPT
Definition random.h:84
A random number generator class that wraps FastLED's random functions.
Definition random.h:25
Fast, efficient random number generators specifically designed for high-performance LED programming.
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
FL_DISABLE_WARNING_PUSH U constexpr common_type_t< T, U > min(T a, U b) FL_NOEXCEPT
Definition math.h:71
unsigned char u8
Definition stdint.h:131
constexpr common_type_t< T, U > max(T a, U b) FL_NOEXCEPT
Definition math.h:75
math::random & default_random()
Global default random number generator instance.
math::random fl_random
Definition random.h:108
fl::u64 u64
Definition s16x16x4.h:221
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT