# Random Numbers

In this lesson we will be looking at Random Numbers. A Random number can be looked at like a flip of a coin or a roll of a die. We can then extend it to more complicated things like selecting a lottery number.

#### Independent Events

something like a coin flip or a dice roll is what statisticians call Independent Events meaning that each try is a new game and it does not depend on any prior results. If you flip a coin you can get tails or heads which we will call 0 and 1. If you get 0 on one flip that in no way indicates what you will get the next time. Even if you 0 many times in a row that does not mean the next flip is more likely to be 1. Rolling a single 6 sided die we can get any of 1,2,3,4,5 or 6 and each one of these numbers is equally likely to come up. Each roll is independent of the previous results. If we have rolled a lot of numbers and not seen a 1 that does not mean a 1 is more likely on the next roll.

#### Average or Expected Value

The Expected Value is the value you expect to get if you repeat the process many times. With the coin flip we expect to get an equal number of tails and heads if we flip many many times.

For the Die roll we can get 1 - 6 over many rolls we should get the average number which is 3.5, of course we can not roll a 3.5 but if we take all the numbers and average that is what we should get. However being independent if our average come out high that does not mean the next roll will be low.

#### Code for Random Numbers

in Arduino and many other languages there is a random number generator

```random(min, max);
```

This will result in a random number between min and max. A small detail is that the max is not included in the results so to get 0 , 1. we use min = 0 and max = 2, so for our coin example we would write

```int coin = random(0,2);
```
`and the single 6 sided die would be `
`int die = random(1,7); `

We can try it with this code

```void setup() {
// put your setup code here, to run once:
Serial.begin(9600);

}

void loop() {
// put your main code here, to run repeatedly:
int coin = random(0,2);
int die = random(1,7);
Serial.print("coin = ");
Serial.print(coin);
Serial.print(" die =  ");
Serial.println(die);
delay(500);

}
```

This just prints random results to the screen. You will notice the structure that we use

```Serial.print( )
```

for most of the time so we print all on one line then the last value we use

```Serial.println( )
```

to print then go to the next line.

```delay(500);
```

is added to slow things down so we can read the numbers on the screen. You can go faster if you like.

#### Code for Random LightShow

Lets apply what we have learned about random numbers to the circuit we built in the previous lesson. We will make a random light show using the 5 LEDs.

Start with the code from the prior lesson but delete what we have in the void loop, so we will start with this:

```const int xStick=A0; //global constants for pin connections
const int yStick=A1;
const int white = 3;
const int blue = 4;
const int green = 5;
const int yellow = 6;
const int red = 7;

void testLED()
{
//Turn them all on to test
digitalWrite(white, 1);
delay(500);
digitalWrite(blue, 1);
delay(500);
digitalWrite(green, 1);
delay(500);
digitalWrite(yellow, 1);
delay(500);
digitalWrite(red, 1);
delay(500);
//Then turn them off
digitalWrite(red, 0);
delay(500);
digitalWrite(yellow, 0);
delay(500);
digitalWrite(green, 0);
delay(500);
digitalWrite(blue, 0);
delay(500);
digitalWrite(white, 0);
delay(500);

}

void lightLEDs(bool redState, bool yellowState, bool greenState, bool blueState, bool whiteState)
{
digitalWrite(red, redState);
digitalWrite(yellow, yellowState);
digitalWrite(green, greenState);
digitalWrite(blue, blueState);
digitalWrite(white, whiteState);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);  //start a serial connection
//Set LED s as output
pinMode(white, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(green, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(red, OUTPUT);

testLED();
}

//function to get the joystick value, returns and integer
int getJoystick(int stickPin)
{
return val;
}

void lightShow(int val)
{
if (val<150) lightLEDs(0,0,0,0,0);
else if (val<300) lightLEDs(1,0,0,0,0);
else if (val<450) lightLEDs(0,1,0,0,0);
else if (val<600) lightLEDs(0,0,1,0,0);
else if (val<750) lightLEDs(0,0,0,1,0);
else  lightLEDs(0,0,0,0,1);
}

void loop() {
// put your main code here, to run repeatedly:

}
```

if you run this code nothing happens, this is expected because we have nothing in the void loop. However we have set up everything we now have all of our lights set up as outputs and tested and we have the lightShow function so we can control 5 LED with one line of code.

#### Code for Random Lights using Switch Case

We will use the random number generator and a switch case structure to write a function to blink lights randomly

```void randomLights()
{
int lights = random(0, 5);
Serial.print("light = ");
Serial.println(lights);
switch (lights) {
case 0:
lightLEDs(1, 0, 0, 0, 0);
break;
case 1:
lightLEDs(0, 1, 0, 0, 0);
break;
case 2:
lightLEDs(0, 0, 1, 0, 0);
break;
case 3:
lightLEDs(0, 0, 0, 1, 0);
break;
case 4:
lightLEDs(0, 0, 0, 0, 1);
break;
default:
// statements
break;
}
}
```

Calling this function from the void loop runs the light show, the randomLights function also print to the screen so you can see what the random number generator is doing.

```void loop() {
// put your main code here, to run repeatedly:
randomLights();
delay(500);
}
```

#### Where do we go from here?

Can you think of games we could make using random numbers? Can you extend these games by combining the LED lights and the Joystick value?

#### Example Code

```const int xStick = A0; //global constants for pin connections
const int yStick = A1;
const int white = 3;
const int blue = 4;
const int green = 5;
const int yellow = 6;
const int red = 7;

void testLED()
{
//Turn them all on to test
digitalWrite(white, 1);
delay(500);
digitalWrite(blue, 1);
delay(500);
digitalWrite(green, 1);
delay(500);
digitalWrite(yellow, 1);
delay(500);
digitalWrite(red, 1);
delay(500);
//Then turn them off
digitalWrite(red, 0);
delay(500);
digitalWrite(yellow, 0);
delay(500);
digitalWrite(green, 0);
delay(500);
digitalWrite(blue, 0);
delay(500);
digitalWrite(white, 0);
delay(500);

}

void lightLEDs(bool redState, bool yellowState, bool greenState, bool blueState, bool whiteState)
{
digitalWrite(red, redState);
digitalWrite(yellow, yellowState);
digitalWrite(green, greenState);
digitalWrite(blue, blueState);
digitalWrite(white, whiteState);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);  //start a serial connection
//Set LED s as output
pinMode(white, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(green, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(red, OUTPUT);

testLED();
}

//function to get the joystick value, returns and integer
int getJoystick(int stickPin)
{
return val;
}

void lightShow(int val)
{
if (val < 150) lightLEDs(0, 0, 0, 0, 0);
else if (val < 300) lightLEDs(1, 0, 0, 0, 0);
else if (val < 450) lightLEDs(0, 1, 0, 0, 0);
else if (val < 600) lightLEDs(0, 0, 1, 0, 0);
else if (val < 750) lightLEDs(0, 0, 0, 1, 0);
else  lightLEDs(0, 0, 0, 0, 1);
}
void randomLights()
{
int lights = random(0, 5);
Serial.print("light = ");
Serial.println(lights);

switch (lights) {

case 0:
lightLEDs(1, 0, 0, 0, 0);
break;
case 1:
lightLEDs(0, 1, 0, 0, 0);
break;
case 2:
lightLEDs(0, 0, 1, 0, 0);
break;
case 3:
lightLEDs(0, 0, 0, 1, 0);
break;
case 4:
lightLEDs(0, 0, 0, 0, 1);
break;
default:
// statements
break;

}

}

void loop() {
// put your main code here, to run repeatedly:
randomLights();
delay(200);
}
```
Back to the course