FastLED 3.9.15
Loading...
Searching...
No Matches
random.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/stdint.h"
4#include "fl/int.h"
5#include "lib8tion/random8.h"
6
7namespace fl {
8
20class fl_random {
21private:
23 u16 seed_;
24
31
35 u32 high = next_random16();
36 u32 low = next_random16();
37 return (high << 16) | low;
38 }
39
40public:
42 typedef u32 result_type;
43
46
49 explicit fl_random(u16 seed) : seed_(seed) {}
50
54 return next_random32();
55 }
56
61 if (n == 0) return 0;
62 u32 r = next_random32();
63 fl::u64 p = (fl::u64)n * (fl::u64)r;
64 return (u32)(p >> 32);
65 }
66
72 result_type delta = max - min;
73 result_type r = (*this)(delta) + min;
74 return r;
75 }
76
79 void set_seed(u16 seed) {
80 seed_ = seed;
81 }
82
85 u16 get_seed() const {
86 return seed_;
87 }
88
91 void add_entropy(u16 entropy) {
92 seed_ += entropy;
93 }
94
97 static constexpr result_type minimum() {
98 return 0;
99 }
100
103 static constexpr result_type maximum() {
104 return 4294967295U;
105 }
106
110 u16 r = next_random16();
111 // return the sum of the high and low bytes, for better mixing
112 return (u8)(((u8)(r & 0xFF)) + ((u8)(r >> 8)));
113 }
114
119 u8 r = random8();
120 r = (r * n) >> 8;
121 return r;
122 }
123
128 u8 random8(u8 min, u8 max) {
129 u8 delta = max - min;
130 u8 r = random8(delta) + min;
131 return r;
132 }
133
136 u16 random16() {
137 return next_random16();
138 }
139
143 u16 random16(u16 n) {
144 return static_cast<u16>((*this)(n));
145 }
146
151 u16 random16(u16 min, u16 max) {
152 return static_cast<u16>((*this)(min, max));
153 }
154};
155
166fl_random& default_random();
167
168} // namespace fl
static constexpr result_type maximum()
Get the maximum value this generator can produce.
Definition random.h:103
static constexpr result_type minimum()
Get the minimum value this generator can produce.
Definition random.h:97
result_type operator()(result_type n)
Generate a random number in the range [0, n)
Definition random.h:60
u16 random16(u16 n)
Generate a 16-bit random number in the range [0, n)
Definition random.h:143
u16 get_seed() const
Get the current seed value for this instance.
Definition random.h:85
fl_random(u16 seed)
Constructor with explicit seed.
Definition random.h:49
u32 result_type
The result type for this random generator (32-bit unsigned integer)
Definition random.h:42
void add_entropy(u16 entropy)
Add entropy to this random number generator instance.
Definition random.h:91
u32 next_random32()
Generate next 32-bit random number using this instance's seed.
Definition random.h:34
u8 random8()
Generate an 8-bit random number.
Definition random.h:109
u8 random8(u8 min, u8 max)
Generate an 8-bit random number in the range [min, max)
Definition random.h:128
result_type operator()()
Generate a random number.
Definition random.h:53
void set_seed(u16 seed)
Set the seed for this random number generator instance.
Definition random.h:79
u16 seed_
The current seed state for this instance.
Definition random.h:23
u8 random8(u8 n)
Generate an 8-bit random number in the range [0, n)
Definition random.h:118
u16 next_random16()
Generate next 16-bit random number using this instance's seed.
Definition random.h:27
u16 random16()
Generate a 16-bit random number.
Definition random.h:136
result_type operator()(result_type min, result_type max)
Generate a random number in the range [min, max)
Definition random.h:71
u16 random16(u16 min, u16 max)
Generate a 16-bit random number in the range [min, max)
Definition random.h:151
fl_random()
Default constructor - uses current global random seed.
Definition random.h:45
LIB8STATIC uint16_t random16_get_seed()
Get the current seed value for the random number generator.
Definition random8.h:100
#define APPLY_FASTLED_RAND16_2053(x)
Multiplies a value by the pseudo-random multiplier.
Definition random8.h:38
#define FASTLED_RAND16_13849
Increment value for pseudo-random number generation.
Definition random8.h:31
unsigned char u8
Definition int.h:17
fl_random & default_random()
Global default random number generator instance.
Definition random.cpp:8
IMPORTANT!
Definition crgb.h:20
Fast, efficient random number generators specifically designed for high-performance LED programming.