Mozzi  version v2.0
sound synthesis library for Arduino
mozzi_rand.h
1 /*
2  * mozzi_rand.h
3  *
4  * This file is part of Mozzi.
5  *
6  * Copyright 2012-2024 Tim Barrass and the Mozzi Team
7  *
8  * Mozzi is licensed under the GNU Lesser General Public Licence (LGPL) Version 2.1 or later.
9  *
10  */
11 
12 #ifndef MOZZI_RAND_H_
13 #define MOZZI_RAND_H_
14 
15 #include <Arduino.h>
16 #include "internal/mozzi_rand_p.h"
17 
31 
41 inline void randSeed(uint32_t seed) { MozziPrivate::randSeed(seed); };
42 
59 inline void randSeed() { MozziPrivate::MozziRandPrivate::autoSeed(); };
60 
66 inline void xorshiftSeed(uint32_t seed) { randSeed(seed); };
67 
74 inline int8_t rand(int8_t minval, int8_t maxval)
75 {
76  return (int8_t) ((((int) (lowByte(xorshift96()))) * (maxval-minval))>>8) + minval;
77 }
78 
84 inline int8_t rand(int8_t maxval)
85 {
86  return (int8_t) ((((int) (lowByte(xorshift96()))) * maxval)>>8);
87 }
88 
95 inline uint8_t rand(uint8_t minval, uint8_t maxval)
96 {
97  return (uint8_t) ((((unsigned int) (lowByte(xorshift96()))) * (maxval-minval))>>8) + minval;
98 }
99 
105 inline uint8_t rand(uint8_t maxval)
106 {
107  return (uint8_t) ((((unsigned int) (lowByte(xorshift96()))) * maxval)>>8);
108 }
109 
118 inline int rand(int minval, int maxval)
119 {
120  return (int) ((((xorshift96() & 0xFFFF) * (maxval-minval))>>16) + minval);
121 }
122 
130 inline int rand(int maxval)
131 {
132  return (int) (((xorshift96() & 0xFFFF) * maxval)>>16);
133 }
134 
141 inline unsigned int rand(unsigned int minval, unsigned int maxval)
142 {
143  return (unsigned int) ((((xorshift96() & 0xFFFF) * (maxval-minval))>>16) + minval);
144 }
145 
153 inline unsigned int rand(unsigned int maxval)
154 {
155  return (unsigned int) (((xorshift96() & 0xFFFF) * maxval)>>16);
156 }
157 
162 inline uint8_t randMidiNote()
163 {
164  return lowByte(xorshift96())>>1;
165 }
166 
167 #endif /* MOZZI_RAND_H_ */
friend uint32_t xorshift96()
Random number generator.
Definition: mozzi_rand.h:30
void xorshiftSeed(uint32_t seed)
Initialises Mozzi's (pseudo)random number generator xorshift96() with a chosen seed number.
Definition: mozzi_rand.h:66
uint8_t randMidiNote()
Generates a random number in the range for midi notes.
Definition: mozzi_rand.h:162
void randSeed(uint32_t seed)
Initialises Mozzi's (pseudo)random number generator xorshift96(), which is used in Mozzi's rand() fun...
Definition: mozzi_rand.h:41
uint32_t xorshift96()
Random number generator.
Definition: mozzi_rand.h:30
int8_t rand(int8_t minval, int8_t maxval)
Ranged random number generator, faster than Arduino's built-in random function, which is too slow for...
Definition: mozzi_rand.h:74