The whole point of a dice is to provide a way to randomly come up with a number. In the case of a D6 dice, this would be a number from 1 to 6. The Arduino library provides a random function we can use, but let's take a look at how it behaves.

Try running the simple sketch below.

```///////////////////////////////////////////////////////////////////////////////
// Circuit Playground Random Demo
//
// Author: Carter Nelson

///////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600);

CircuitPlayground.begin();
}

///////////////////////////////////////////////////////////////////////////////
void loop() {
// Wait for button press
while (!CircuitPlayground.leftButton()   &&
!CircuitPlayground.rightButton())  {
// Do nothing, just waiting for a button press...
}

// Print a random number
Serial.println(random(1,7));

// Debounce delay
delay(500);
}```

With this code loaded and running on the Circuit Playground, open the Serial Monitor.

Tools -> Serial Monitor

and then press either button. Each time, a random number from 1 to 6 will be printed out. Let me guess, you got the same sequence I did as shown below.

And if you reset the Circuit Playground and try this again, you will get the same sequence again. So what's going on?

In turns out that the `random()` function implemented in the Arduino library is only a pseudo-random function. This simply means it isn't fully random (pseudo = false). It just produces a random like sequence of numbers, and the same sequence every time.

To get around this, we need to initialize the random function with a random value. Which, to be honest, sounds a little...unusual! I mean, if we had a random value why use random() at all? But it really does make sense: if it were possible to get one random value, maybe in a round-about way, we could then take advantage of the simple built in function.

This is called seeding the function and the value is called the seed. You plant a seed of randomness, and then we can harvest any number of random numbers we need, forever!

But where can we come up with a random seed value? One way is to use some unused sensor that is (hopefully) reporting random and changing values of whatever it is meant to sense. Like light. Hey, we got a light sensor. Let's use that. We simply need to make a call to `lightSensor()` and feed that value into `randomSeed()`.

Here's a new version of the code that includes a call to `randomSeed()` in the `setup()`. This seed is generated by reading the light sensor.

```///////////////////////////////////////////////////////////////////////////////
// Circuit Playground Random Demo with Seed
//
// Author: Carter Nelson

///////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600);

CircuitPlayground.begin();

// Seed the random function with light sensor value
randomSeed(CircuitPlayground.lightSensor());
}

///////////////////////////////////////////////////////////////////////////////
void loop() {
// Wait for button press
while (!CircuitPlayground.leftButton()   &&
!CircuitPlayground.rightButton())  {
// Do nothing, just waiting for a button press...
}

// Print a random number
Serial.println(random(1,7));

// Debounce delay
delay(500);
}```

Load this code, open the Serial Monitor, and try again by pressing the buttons. Hopefully you get a different sequence this time, and it's different than the one I got.

While this isn't perfect, it will work for our needs. This is what we will use to generate the random number to simulate a dice roll

This guide was first published on Dec 16, 2016. It was last updated on Mar 08, 2024.

This page (Random, or Not?) was last updated on Mar 08, 2024.