Mozzi  version v1.1.0
sound synthesis library for Arduino
Random

Fast random number generator functions. More...

Functions

unsigned long xorshift96 ()
 Random number generator. More...
 
void randSeed (long 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 (long 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...
 
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...
 
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...
 
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...
 
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 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 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...
 

Detailed Description

Fast random number generator functions.

These replace Arduino random() which is so slow it will stop your audio. They can even be used to generate audio noise.

Function Documentation

◆ rand() [1/8]

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.

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

Definition at line 171 of file mozzi_rand.cpp.

◆ rand() [2/8]

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.

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

Definition at line 183 of file mozzi_rand.cpp.

◆ rand() [3/8]

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.

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.

Definition at line 195 of file mozzi_rand.cpp.

◆ rand() [4/8]

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.

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 207 of file mozzi_rand.cpp.

◆ rand() [5/8]

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.

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

Definition at line 218 of file mozzi_rand.cpp.

◆ rand() [6/8]

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.

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

Definition at line 229 of file mozzi_rand.cpp.

◆ rand() [7/8]

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.

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.

Definition at line 240 of file mozzi_rand.cpp.

◆ rand() [8/8]

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.

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.

Definition at line 251 of file mozzi_rand.cpp.

◆ randMidiNote()

uint8_t randMidiNote ( )

Generates a random number in the range for midi notes.

Returns
a random value between 0 and 127 inclusive

Definition at line 261 of file mozzi_rand.cpp.

◆ 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 ( long  seed)

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 53 of file mozzi_rand.cpp.

◆ randSeed() [2/2]

void randSeed ( )

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
It's not perfect, as discussed in the forum thread. It might only work with some processors: (from the thread) "...ATmega328P in DIP, possibly others but the duemilanove and uno will do it at least." So far, gizduino's AVR_ATmega644P chip doesn't like it, so we use (long)analogRead(0)*analogRead(1) for that instead. It works to some degree on STM32 chips, but the produced seed is not very random at all. Again, using an appropriate analogRead() (preferably on one or two floating input pins) is much more effective.

Definition at line 117 of file mozzi_rand.cpp.

◆ xorshift96()

unsigned long xorshift96 ( )

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 26 of file mozzi_rand.cpp.

◆ xorshiftSeed()

void xorshiftSeed ( long  seed)

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

Parameters
seeda number to use as a seed.

Definition at line 158 of file mozzi_rand.cpp.