Mozzi  alpha 0.01.1t
sound synthesis library for Arduino
 All Classes Functions Typedefs
Functions
fast random number generator functions

Functions

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

Function Documentation

char rand ( char  minval,
char  maxval 
)

Ranged random number generator, faster than Arduino's built-in random function, which is too slow for 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 char between minval and maxval-1 inclusive.

Definition at line 123 of file mozzi_rand.cpp.

unsigned char rand ( unsigned char  minval,
unsigned char  maxval 
)

Ranged random number generator, faster than Arduino's built-in random function, which is too slow for 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 char between minval and maxval-1 inclusive.

Definition at line 135 of file mozzi_rand.cpp.

int rand ( int  minval,
int  maxval 
)

Ranged random number generator, faster than Arduino's built-in random function, which is too slow for 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 147 of file mozzi_rand.cpp.

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

char rand ( char  maxval)

Ranged random number generator, faster than Arduino's built-in random function, which is too slow for 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 char between 0 and maxval-1 inclusive.

Definition at line 170 of file mozzi_rand.cpp.

unsigned char rand ( unsigned char  maxval)

Ranged random number generator, faster than Arduino's built-in random function, which is too slow for 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 char between 0 and maxval-1 inclusive.

Definition at line 181 of file mozzi_rand.cpp.

int rand ( int  maxval)

Ranged random number generator, faster than Arduino's built-in random function, which is too slow for 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 192 of file mozzi_rand.cpp.

unsigned int rand ( unsigned int  maxval)

Ranged random number generator, faster than Arduino's built-in random function, which is too slow for 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 203 of file mozzi_rand.cpp.

unsigned char randMidiNote ( )

Generates a random number in the range for midi notes.

Returns:
a random value between 0 and 127 inclusive

Definition at line 213 of file mozzi_rand.cpp.

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 utils.h, because it takes up memory which might be needed for other things. You need to "#include <primes.h>" separately to utils.h.

Definition at line 58 of file primes.h.

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 utils.h, because it takes up memory which might be needed for other things. You need to "#include <primes.h>" separately to utils.h.

Definition at line 73 of file primes.h.

void randSeed ( unsigned 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:
seedan int to use as a seed.

Definition at line 44 of file mozzi_rand.cpp.

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:
IMPORTANT!!! Make sure you set analogReference() to suit your sketch after calling randSeed(). randSeed() sets analogReference to DEFAULT. Sorry about that.
This is quite slow, so best to call it only in setup().
It's not perfect, as discussed in the forum thread, but probably a pretty good option.
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.

Definition at line 94 of file mozzi_rand.cpp.

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.
Todo:
check timing of xorshift96(), rand() and other PRNG candidates.

Definition at line 17 of file mozzi_rand.cpp.

void xorshiftSeed ( unsigned long  seed)

Initialises Mozzi's (pseudo)random number generator xorshift96().

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().

Parameters:
seedan int to use as a seed.

Definition at line 110 of file mozzi_rand.cpp.