Mozzi  alpha 0.01.1t
sound synthesis library for Arduino
 All Classes Functions Typedefs
mozzi_rand.cpp
00001 #include "mozzi_rand.h"
00002 
00003 // moved these out of xorshift96() so xorshift96() can be reseeded manually
00004 static unsigned long x=132456789, y=362436069, z=521288629;
00005 // static unsigned long x= analogRead(A0)+123456789;
00006 // static unsigned long y= analogRead(A1)+362436069;
00007 // static unsigned long z= analogRead(A2)+521288629;
00008 
00017 unsigned long xorshift96()
00018 { //period 2^96-1
00019                 // static unsigned long x=123456789, y=362436069, z=521288629;
00020                 unsigned long t;
00021 
00022                 x ^= x << 16;
00023                 x ^= x >> 5;
00024                 x ^= x << 1;
00025 
00026                 t = x;
00027                 x = y;
00028                 y = z;
00029                 z = t ^ x ^ y;
00030 
00031                 return z;
00032 }
00033 
00034 
00044 void randSeed(unsigned long seed)
00045 {
00046                 x=seed;
00047 }
00048 
00049 
00050 #if !defined (__AVR_ATmega644P__)
00051 /*
00052 longRandom(), used as a seed generator, comes from:
00053 http://arduino.cc/forum/index.php/topic,38091.0.html
00054 //  AUTHOR: Rob Tillaart
00055 // PURPOSE: Simple Random functions based upon unreliable internal temp sensor
00056 // VERSION: 0.1
00057 //       DATE: 2011-05-01
00058 //
00059 // Released to the public domain, use at own risk
00060 //
00061 */
00062 static long longRandom()
00063 {
00064   analogReference(INTERNAL);
00065   unsigned long rv = 0;
00066   for (byte i=0; i< 32; i++) rv |= (analogRead(8) & 1L) << i;
00067   return rv;
00068 }
00069 #else
00070 // a less fancy version for gizduino (__AVR_ATmega644P__) which doesn't know INTERNAL
00071 static long longRandom()
00072 {
00073   return (long)analogRead(0)*analogRead(1);
00074 }
00075 #endif
00076 
00077 
00094 void randSeed() {
00095                 x=longRandom();
00096                 y=longRandom();
00097                 z=longRandom();
00098                 analogReference(DEFAULT);
00099 }
00100 
00101 
00102 
00110 void xorshiftSeed(unsigned long seed)
00111 {
00112                 x=seed;
00113 }
00114 
00115 
00116 
00123 char rand(char minval, char maxval)
00124 {
00125                 return (char) ((((int) (lowByte(xorshift96()))) * (maxval-minval))/256) + minval;
00126 }
00127 
00128 
00135 unsigned char rand(unsigned char minval, unsigned char maxval)
00136 {
00137                 return (unsigned char) ((((unsigned int) (lowByte(xorshift96()))) * (maxval-minval))/256) + minval;
00138 }
00139 
00140 
00147 int rand( int minval,  int maxval)
00148 {
00149                 return (int) ((((xorshift96()>>16) * (maxval-minval))>>16) + minval);
00150 }
00151 
00152 
00159 unsigned int rand(unsigned int minval, unsigned int maxval)
00160 {
00161                 return (unsigned int) ((((xorshift96()>>16) * (maxval-minval))>>16) + minval);
00162 }
00163 
00164 
00170 char rand(char maxval)
00171 {
00172                 return (char) ((((int) (lowByte(xorshift96()))) * maxval)/256);
00173 }
00174 
00175 
00181 unsigned char rand(unsigned char maxval)
00182 {
00183                 return (unsigned char) ((((unsigned int) (lowByte(xorshift96()))) * maxval)/256);
00184 }
00185 
00186 
00192 int rand(int maxval)
00193 {
00194                 return (int) (((xorshift96()>>16) * maxval)>>16);
00195 }
00196 
00197 
00203 unsigned int rand(unsigned int maxval)
00204 {
00205                 return (unsigned int) (((xorshift96()>>16) * maxval)>>16);
00206 }
00207 
00208 
00213 unsigned char randMidiNote()
00214 {
00215                 return lowByte(xorshift96())>>1;
00216 }
00217 
00218