aboutsummaryrefslogtreecommitdiff
path: root/lib/lib8tion/random8.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/lib8tion/random8.h')
-rw-r--r--lib/lib8tion/random8.h94
1 files changed, 94 insertions, 0 deletions
diff --git a/lib/lib8tion/random8.h b/lib/lib8tion/random8.h
new file mode 100644
index 000000000..7ee67cbb3
--- /dev/null
+++ b/lib/lib8tion/random8.h
@@ -0,0 +1,94 @@
1#ifndef __INC_LIB8TION_RANDOM_H
2#define __INC_LIB8TION_RANDOM_H
3///@ingroup lib8tion
4
5///@defgroup Random Fast random number generators
6/// Fast 8- and 16- bit unsigned random numbers.
7/// Significantly faster than Arduino random(), but
8/// also somewhat less random. You can add entropy.
9///@{
10
11// X(n+1) = (2053 * X(n)) + 13849)
12#define FASTLED_RAND16_2053 ((uint16_t)(2053))
13#define FASTLED_RAND16_13849 ((uint16_t)(13849))
14
15/// random number seed
16extern uint16_t rand16seed;// = RAND16_SEED;
17
18/// Generate an 8-bit random number
19LIB8STATIC uint8_t random8(void)
20{
21 rand16seed = (rand16seed * FASTLED_RAND16_2053) + FASTLED_RAND16_13849;
22 // return the sum of the high and low bytes, for better
23 // mixing and non-sequential correlation
24 return (uint8_t)(((uint8_t)(rand16seed & 0xFF)) +
25 ((uint8_t)(rand16seed >> 8)));
26}
27
28/// Generate a 16 bit random number
29LIB8STATIC uint16_t random16(void)
30{
31 rand16seed = (rand16seed * FASTLED_RAND16_2053) + FASTLED_RAND16_13849;
32 return rand16seed;
33}
34
35/// Generate an 8-bit random number between 0 and lim
36/// @param lim the upper bound for the result
37LIB8STATIC uint8_t random8_max(uint8_t lim)
38{
39 uint8_t r = random8();
40 r = (r*lim) >> 8;
41 return r;
42}
43
44/// Generate an 8-bit random number in the given range
45/// @param min the lower bound for the random number
46/// @param lim the upper bound for the random number
47LIB8STATIC uint8_t random8_min_max(uint8_t min, uint8_t lim)
48{
49 uint8_t delta = lim - min;
50 uint8_t r = random8_max(delta) + min;
51 return r;
52}
53
54/// Generate an 16-bit random number between 0 and lim
55/// @param lim the upper bound for the result
56LIB8STATIC uint16_t random16_max(uint16_t lim)
57{
58 uint16_t r = random16();
59 uint32_t p = (uint32_t)lim * (uint32_t)r;
60 r = p >> 16;
61 return r;
62}
63
64/// Generate an 16-bit random number in the given range
65/// @param min the lower bound for the random number
66/// @param lim the upper bound for the random number
67LIB8STATIC uint16_t random16_min_max( uint16_t min, uint16_t lim)
68{
69 uint16_t delta = lim - min;
70 uint16_t r = random16_max(delta) + min;
71 return r;
72}
73
74/// Set the 16-bit seed used for the random number generator
75LIB8STATIC void random16_set_seed(uint16_t seed)
76{
77 rand16seed = seed;
78}
79
80/// Get the current seed value for the random number generator
81LIB8STATIC uint16_t random16_get_seed(void)
82{
83 return rand16seed;
84}
85
86/// Add entropy into the random number generator
87LIB8STATIC void random16_add_entropy(uint16_t entropy)
88{
89 rand16seed += entropy;
90}
91
92///@}
93
94#endif