GCC Code Coverage Report


Directory: ./
File: overlays/wled/lib8tion/random8.h
Date: 2026-10-29 08:11:58
Exec Total Coverage
Lines: 17 17 100.0%
Functions: 6 6 100.0%
Branches: 0 0 -%

Line Branch Exec Source
1 #ifndef __INC_LIB8TION_RANDOM_H
2 #define __INC_LIB8TION_RANDOM_H
3
4 #include <stdint.h>
5
6 #include "../lib8tion/lib8static.h"
7
8 /// @file random8.h
9 /// Fast, efficient random number generators specifically
10 /// designed for high-performance LED programming.
11
12 /// @ingroup lib8tion
13 /// @{
14
15 /// @defgroup Random Fast Random Number Generators
16 /// Fast 8-bit and 16-bit unsigned random number generators.
17 /// Significantly faster than Arduino random(), but
18 /// also somewhat less random. You can add entropy.
19 ///
20 /// Pseudo-random number generation follows the form:
21 /// @code
22 /// X(n+1) = (2053 * X(n)) + 13849)
23 /// @endcode
24 /// @{
25
26 /// Multiplier value for pseudo-random number generation
27 #define FASTLED_RAND16_2053 ((uint16_t)(2053))
28 /// Increment value for pseudo-random number generation
29 #define FASTLED_RAND16_13849 ((uint16_t)(13849))
30
31 #if defined(LIB8_ATTINY)
32 /// Multiplies a value by the pseudo-random multiplier
33 #define APPLY_FASTLED_RAND16_2053(x) (x << 11) + (x << 2) + x
34 #else
35 /// Multiplies a value by the pseudo-random multiplier
36 #define APPLY_FASTLED_RAND16_2053(x) (x * FASTLED_RAND16_2053)
37 #endif
38
39 /// Seed for the random number generator functions
40 extern uint16_t rand16seed; // = RAND16_SEED;
41
42 /// Generate an 8-bit random number
43 /// @returns random 8-bit number, in the range 0-255
44 174268 LIB8STATIC uint8_t random8() {
45 174268 rand16seed = APPLY_FASTLED_RAND16_2053(rand16seed) + FASTLED_RAND16_13849;
46 // return the sum of the high and low bytes, for better
47 // mixing and non-sequential correlation
48 174268 return (uint8_t)(((uint8_t)(rand16seed & 0xFF)) +
49 174268 ((uint8_t)(rand16seed >> 8)));
50 }
51
52 /// Generate a 16-bit random number
53 /// @returns random 16-bit number, in the range 0-65535
54 17693 LIB8STATIC uint16_t random16() {
55 17693 rand16seed = APPLY_FASTLED_RAND16_2053(rand16seed) + FASTLED_RAND16_13849;
56 17693 return rand16seed;
57 }
58
59 /// Generate an 8-bit random number between 0 and lim
60 /// @param lim the upper bound for the result, exclusive
61 22581 LIB8STATIC uint8_t random8(uint8_t lim) {
62 22581 uint8_t r = random8();
63 22581 r = (r * lim) >> 8;
64 22581 return r;
65 }
66
67 /// Generate an 8-bit random number in the given range
68 /// @param min the lower bound for the random number, inclusive
69 /// @param lim the upper bound for the random number, exclusive
70 48 LIB8STATIC uint8_t random8(uint8_t min, uint8_t lim) {
71 48 uint8_t delta = lim - min;
72 48 uint8_t r = random8(delta) + min;
73 48 return r;
74 }
75
76 /// Generate an 16-bit random number between 0 and lim
77 /// @param lim the upper bound for the result, exclusive
78 LIB8STATIC uint16_t random16(uint16_t lim) {
79 uint16_t r = random16();
80 uint32_t p = (uint32_t)lim * (uint32_t)r;
81 r = p >> 16;
82 return r;
83 }
84
85 /// Generate an 16-bit random number in the given range
86 /// @param min the lower bound for the random number, inclusive
87 /// @param lim the upper bound for the random number, exclusive
88 LIB8STATIC uint16_t random16(uint16_t min, uint16_t lim) {
89 uint16_t delta = lim - min;
90 uint16_t r = random16(delta) + min;
91 return r;
92 }
93
94 /// Set the 16-bit seed used for the random number generator
95 96 LIB8STATIC void random16_set_seed(uint16_t seed) { rand16seed = seed; }
96
97 /// Get the current seed value for the random number generator
98 54 LIB8STATIC uint16_t random16_get_seed() { return rand16seed; }
99
100 /// Add entropy into the random number generator
101 LIB8STATIC void random16_add_entropy(uint16_t entropy) {
102 rand16seed += entropy;
103 }
104
105 /// @} Random
106 /// @} lib8tion
107
108 #endif
109