MCPcopy Create free account
hub / github.com/FastLED/FastLED / random

Class random

src/fl/math/random.h:25–103  ·  view source on GitHub ↗

@brief A random number generator class that wraps FastLED's random functions This class provides a standard C++ random generator interface that can be used with algorithms like fl::shuffle. Each instance maintains its own seed state independent of other instances and the global FastLED random state. All public methods delegate to private _nolock helpers. This means external locking (via the Lock

Source from the content-addressed store, hash-verified

23/// fl::shuffle(vec.begin(), vec.end(), rng);
24/// @endcode
25class random {
26public:
27 typedef u32 result_type;
28
29 /// Default constructor - uses current global random seed
30 random() FL_NOEXCEPT : mSeed(random16_get_seed()) {}
31
32 /// Constructor with explicit seed
33 explicit random(u16 seed) FL_NOEXCEPT : mSeed(seed) {}
34
35 result_type operator()() FL_NOEXCEPT { return generate_nolock(); }
36 result_type operator()(result_type n) FL_NOEXCEPT { return generate_nolock(n); }
37 result_type operator()(result_type min, result_type max) FL_NOEXCEPT { return generate_nolock(min, max); }
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
46 u8 random8() FL_NOEXCEPT { return random8_nolock(); }
47 u8 random8(u8 n) FL_NOEXCEPT { return random8_nolock(n); }
48 u8 random8(u8 min, u8 max) FL_NOEXCEPT { return random8_nolock(min, max); }
49
50 u16 random16() FL_NOEXCEPT { return random16_nolock(); }
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
57 u16 next_random16_nolock() FL_NOEXCEPT {
58 mSeed = APPLY_FASTLED_RAND16_2053(mSeed) + FASTLED_RAND16_13849;
59 return mSeed;
60 }
61
62 u32 next_random32_nolock() FL_NOEXCEPT {
63 u32 high = next_random16_nolock();
64 u32 low = next_random16_nolock();
65 return (high << 16) | low;
66 }
67
68 result_type generate_nolock() FL_NOEXCEPT {
69 return next_random32_nolock();
70 }
71
72 result_type generate_nolock(result_type n) FL_NOEXCEPT {
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
79 result_type generate_nolock(result_type min, result_type max) FL_NOEXCEPT {
80 result_type delta = max - min;
81 return generate_nolock(delta) + min;
82 }

Callers 8

triggerRippleFunction · 0.50
processAutoTriggerFunction · 0.50
drawMatrixRainFunction · 0.50
drawFireEffectFunction · 0.50
loopFunction · 0.50
PaintVuNotesMethod · 0.50
triggerRippleFunction · 0.50
advanceMethod · 0.50

Calls

no outgoing calls

Tested by

no test coverage detected