Random Number Generation

Wink setup, hardware, basic programming. User to user support.
wclift
Posts: 32
Joined: Sun Feb 07, 2016 6:10 am

Random Number Generation

Postby wclift » Fri Feb 19, 2016 8:06 am

As a side outcome of another thread, I decided to play with Arduino random number generation, to help clear up some confusion.

1) The Arduino documentation describes the native random() function:
https://www.arduino.cc/en/Reference/Random
random() can take either one or two arguments. Here are two examples:

Code: Select all

  // create a random number from 0 to 299
  randNumber = random(300);

  // create a random number from 10 to 19
  randNumber = random(10, 20);

The important thing to note is that it returns an integer up to max-1. So, if you want it to return only two values 0 and 1, then do this:

Code: Select all

random(0,2)  or random(2)


2) The sequence returned by random() is only pseudo-random, meaning that given the same starting "seed", the random() function will return the identical string of random values every time. This is really useful for testing initial programs, but when you want a different random pattern every time, you need a way to provide a different "random" seed to the random number generator. You can set the random seed at any time using this function:

Code: Select all

randomSeed(your_value);

If you use a constant value to the seed function, you will again get a new sequence, but that is the same each time you run. So, you need a way to feed a "random" value to the randomSeed() function.

3) One good source of variable "random" values is to read an Arduino input pin. How do you do that? First, look at WinkHardware.h in the Pin Defines section. You'll see these defines:

Code: Select all

#define LineSenseLeft A5
#define LineSenseRight A4
#define AmbientSenseLeft A3
#define AmbientSenseCenter A2
#define AmbientSenseRight A1

This tells you that Arduino pins A1-A5 are connected to Winks Ambient and Line Sensors. The define allows you to use a sensor name rather than a pin name, for clarity in your code. So, do something like this:

Code: Select all

  randomSeed(analogRead(A2));
or
  randomSeed(analogRead(AmbientSenseCenter));



4) Here's my simple code to test and display test seeds and random values:

Code: Select all

/*

  Wink Robot
  Using random() function effectively
  Wayne Clift Feb '16

*/

#include "WinkHardware.h"

/* set up hardware */
void setup() {
  hardwareBegin();        //initialize Wink's brain to work with his circuitry
  playStartChirp();       //Play startup chirp and blink eyes

  randomSeed(analogRead(A2));  //for the random number generator, but doesnt work well
}

int rndm;

void loop() {
  rndm = random(0, 2);
  delay(500);

  // dump rndm to serial to see if its picking different values
  Serial.print(analogRead(A2));    //
  Serial.print("\t");              //print a couple blank spaces
  Serial.print(analogRead(B0));
  Serial.print("\t");              //print a couple blank spaces
  Serial.print(rndm);              //print sensorValue through serial port
  Serial.print("\t");              //print a couple blank spaces
  Serial.println();                //print a blank line feed

}


The first two values in a row are raw reads of the A2 and B0 pin. I don't know what B0 is connected to. You will see that A2 can be made to vary between 0-1023 by making it lighter or darker. B0 has very little variation. And the third column is a stream of random numbers either 0 or 1.

You can play with and manipulate Wink in light or dark all you want, and the random value stream is unaffected. it will continue to provide a random pattern of values. And the seed function in setup() will cause the random sequence to have high likelihood of being different each time. If you wanted to ensure that even more, you could always place the randomSeed() function inside loop() and resample a new seed frequently. But that's probably overkill.

Have fun using random(). Try out this sample code to confirm that you can get a random series of values, then begin using it in your code. You can see examples of random in the WinkPreLoaded sketch (though it doesn't use randomSeed()).

Good luck and post your experience or questions!

Who is online

Users browsing this forum: No registered users and 1 guest