Mozzi  version v2.0
sound synthesis library for Arduino
Fast random number generator functions

These replace Arduino random() which is so slow it will stop your audio. More...

Detailed Description

These replace Arduino random() which is so slow it will stop your audio.

They can even be used to generate audio noise.

Functions

uint32_t xorshift96 ()
 Random number generator. More...
 
void randSeed (uint32_t seed)
 Initialises Mozzi's (pseudo)random number generator xorshift96(), which is used in Mozzi's rand() function. More...
 
void randSeed ()
 Initialises Mozzi's (pseudo)random number generator xorshift96(), which is used in Mozzi's rand() function. More...
 
void xorshiftSeed (uint32_t seed)
 Initialises Mozzi's (pseudo)random number generator xorshift96() with a chosen seed number. More...
 
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 generating at audio rate with Mozzi. More...
 
int8_t rand (int8_t maxval)
 Ranged random number generator, faster than Arduino's built-in random function, which is too slow for generating at audio rate with Mozzi. More...
 
uint8_t rand (uint8_t minval, uint8_t maxval)
 Ranged random number generator, faster than Arduino's built-in random function, which is too slow for generating at audio rate with Mozzi. More...
 
uint8_t rand (uint8_t maxval)
 Ranged random number generator, faster than Arduino's built-in random function, which is too slow for generating at audio rate with Mozzi. More...
 
int rand (int minval, int maxval)
 Ranged random number generator, faster than Arduino's built-in random function, which is too slow for generating at audio rate with Mozzi. More...
 
int rand (int maxval)
 Ranged random number generator, faster than Arduino's built-in random function, which is too slow for generating at audio rate with Mozzi. More...
 
unsigned int rand (unsigned int minval, unsigned int maxval)
 Ranged random number generator, faster than Arduino's built-in random function, which is too slow for generating at audio rate with Mozzi. More...
 
unsigned int rand (unsigned int maxval)
 Ranged random number generator, faster than Arduino's built-in random function, which is too slow for generating at audio rate with Mozzi. More...
 
uint8_t randMidiNote ()
 Generates a random number in the range for midi notes. More...
 
unsigned int randPrime (unsigned int n)
 Generates a random prime number between 0 and the n-1th prime number. More...
 
unsigned int randPrimeUpTo (unsigned int n)
 Generates a random prime number between 0 and the given input number inclusive. More...
 

Function Documentation

◆ rand() [1/8]

int rand ( int  maxval)
inline

Ranged random number generator, faster than Arduino's built-in random function, which is too slow for generating at audio rate with Mozzi.

Parameters
maxvalthe maximum signed int value of the range to be chosen from. Maxval-1 will be the largest value possibly returned by the function.
Returns
a random int between 0 and maxval-1 inclusive.
Note
The returned value is always in the 16 bit range, even on platforms where int is wider. If you need 32 bits, call xorshift96(), directly.

Definition at line 130 of file mozzi_rand.h.

◆ rand() [2/8]

int rand ( int  minval,
int  maxval 
)
inline

Ranged random number generator, faster than Arduino's built-in random function, which is too slow for generating at audio rate with Mozzi.

Parameters
minvalthe minimum signed int value of the range to be chosen from. Minval will be the minimum value possibly returned by the function.
maxvalthe maximum signed int value of the range to be chosen from. Maxval-1 will be the largest value possibly returned by the function.
Returns
a random int between minval and maxval-1 inclusive.
Note
The returned value is always in the 16 bit range, even on platforms where int is wider. If you need 32 bits, call xorshift96(), directly.

Definition at line 118 of file mozzi_rand.h.

◆ rand() [3/8]

int8_t rand ( int8_t  maxval)
inline

Ranged random number generator, faster than Arduino's built-in random function, which is too slow for generating at audio rate with Mozzi.

Parameters
maxvalthe maximum signed byte value of the range to be chosen from. Maxval-1 will be the largest value possibly returned by the function.
Returns
a random signed byte between 0 and maxval-1 inclusive.

Definition at line 84 of file mozzi_rand.h.

◆ rand() [4/8]

int8_t rand ( int8_t  minval,
int8_t  maxval 
)
inline

Ranged random number generator, faster than Arduino's built-in random function, which is too slow for generating at audio rate with Mozzi.

Parameters
minvalthe minimum signed byte value of the range to be chosen from. Minval will be the minimum value possibly returned by the function.
maxvalthe maximum signed byte value of the range to be chosen from. Maxval-1 will be the largest value possibly returned by the function.
Returns
a random signed byte between minval and maxval-1 inclusive.

Definition at line 74 of file mozzi_rand.h.

◆ rand() [5/8]

uint8_t rand ( uint8_t  maxval)
inline

Ranged random number generator, faster than Arduino's built-in random function, which is too slow for generating at audio rate with Mozzi.

Parameters
maxvalthe maximum unsigned byte value of the range to be chosen from. Maxval-1 will be the largest value possibly returned by the function.
Returns
a random unsigned byte between 0 and maxval-1 inclusive.

Definition at line 105 of file mozzi_rand.h.

◆ rand() [6/8]

uint8_t rand ( uint8_t  minval,
uint8_t  maxval 
)
inline

Ranged random number generator, faster than Arduino's built-in random function, which is too slow for generating at audio rate with Mozzi.

Parameters
minvalthe minimum unsigned byte value of the range to be chosen from. Minval will be the minimum value possibly returned by the function.
maxvalthe maximum unsigned byte value of the range to be chosen from. Maxval-1 will be the largest value possibly returned by the function.
Returns
a random unsigned byte between minval and maxval-1 inclusive.

Definition at line 95 of file mozzi_rand.h.

◆ rand() [7/8]

unsigned int rand ( unsigned int  maxval)
inline

Ranged random number generator, faster than Arduino's built-in random function, which is too slow for generating at audio rate with Mozzi.

Parameters
maxvalthe maximum unsigned int value of the range to be chosen from. Maxval-1 will be the largest value possibly returned by the function.
Returns
a random unsigned int between 0 and maxval-1 inclusive.
Note
The returned value is always in the 16 bit range, even on platforms where int is wider. If you need 32 bits, call xorshift96(), directly.

Definition at line 153 of file mozzi_rand.h.

◆ rand() [8/8]

unsigned int rand ( unsigned int  minval,
unsigned int  maxval 
)
inline

Ranged random number generator, faster than Arduino's built-in random function, which is too slow for generating at audio rate with Mozzi.

Parameters
minvalthe minimum unsigned int value of the range to be chosen from. Minval will be the minimum value possibly returned by the function.
maxvalthe maximum unsigned int value of the range to be chosen from. Maxval-1 will be the largest value possibly returned by the function.
Returns
a random unsigned int between minval and maxval-1 inclusive.

Definition at line 141 of file mozzi_rand.h.

◆ randMidiNote()

uint8_t randMidiNote ( )
inline

Generates a random number in the range for midi notes.

Returns
a random value between 0 and 127 inclusive

Definition at line 162 of file mozzi_rand.h.

◆ randPrime()

unsigned int randPrime ( unsigned int  n)
inline

Generates a random prime number between 0 and the n-1th prime number.

This uses a stored array of primes, which takes about 2.5k of progmem.

Parameters
nthe maximum index in the series of primes up to which numbers will be returned. The maximum is 1128.
Returns
random prime number between 0 and n-1th index in the series of primes.
Note
This isn't included automatically with mozzi_rand.h, because it takes up memory which might be needed for other things. You need to "#include <primes.h>" separately to mozzi_rand.h.

Definition at line 49 of file primes.h.

◆ randPrimeUpTo()

unsigned int randPrimeUpTo ( unsigned int  n)
inline

Generates a random prime number between 0 and the given input number inclusive.

This uses a stored array of primes up to 10000, which takes about 2.5k of progmem.

Parameters
nthe upper limit of the random prime number to be generated. The maximum is 10000.
Returns
random prime number between 0 and n.
Note
This isn't included automatically with mozzi_utils.h, because it takes up memory which might be needed for other things. You need to "#include <primes.h>" separately to mozzi_utils.h.

Definition at line 64 of file primes.h.

◆ randSeed() [1/2]

void randSeed ( )
inline

Initialises Mozzi's (pseudo)random number generator xorshift96(), which is used in Mozzi's rand() function.

This can be useful if you want random sequences to be different on each run of a sketch, by seeding with a fairly random input. randSeed() called without a parameter uses noise from reading the Arduino's internal temperature as the seed, a technique discussed at http://arduino.cc/forum/index.php/topic,38091.0.html, borrowing code put there by Rob Tillaart.

Note
Intialization of the random seed is done differently on different MCUs, but is nowhere near perfect for most (and for some it is not even implemented at all). Many implementations (e.g. on AVR, STM32) simply rely on reading a (hopefully noisy) internal temperature sensor. You will often get better results by calling analogRead() - not mozziAnalogRead(0), in this case! - on one or two floating (non-connected) analog pins.

Definition at line 59 of file mozzi_rand.h.

◆ randSeed() [2/2]

void randSeed ( uint32_t  seed)
inline

Initialises Mozzi's (pseudo)random number generator xorshift96(), which is used in Mozzi's rand() function.

This can be useful if you want random sequences to be different on each run of a sketch, by seeding with fairly random input, such as analogRead() on an unconnected pin (as explained in the Arduino documentation for randomSeed(). randSeed is the same as xorshift96Seed(), but easier to remember.

Parameters
seeda number to use as a seed.

Definition at line 41 of file mozzi_rand.h.

◆ xorshift96()

uint32_t xorshift96 ( )
inline

Random number generator.

A faster replacement for Arduino's random function, which is too slow to use with Mozzi. Based on Marsaglia, George. (2003). Xorshift RNGs. http://www.jstatsoft.org/v08/i14/xorshift.pdf

Returns
a random 32 bit integer.

Definition at line 30 of file mozzi_rand.h.

◆ xorshiftSeed()

void xorshiftSeed ( uint32_t  seed)
inline

Initialises Mozzi's (pseudo)random number generator xorshift96() with a chosen seed number.

Parameters
seeda number to use as a seed. TODO: duplicate deprecate / remove

Definition at line 66 of file mozzi_rand.h.