Introduction
A sketch does exactly the same thing every time it runs. That is usually the point — but a dice that always rolls four is not a dice, and a game whose next move you can predict is not a game.
random() gives you a number you did not choose. Strictly it is pseudo-random: a formula that produces a sequence so irregular it looks random, but which starts from the same place every time the board powers up. Run the same sketch twice and you get the same "random" numbers in the same order.
randomSeed() fixes that, by starting the formula somewhere unpredictable. The usual trick is to read an unconnected analog pin, which floats and picks up electrical noise — the very thing that made a floating input a problem for buttons is what makes it useful here.
Two ideas worth keeping apart: random means you cannot predict it, and unique means it does not repeat. random() gives you the first, never the second. A dice can roll three sixes in a row, and so can this.
What you will be able to do
By the end of this lesson you can:
- Get a random number in a range with
random(). - Say exactly which numbers
random(1, 7)can return, and which it cannot. - Explain why an unseeded sketch repeats itself on every reset.
- Seed the generator with
randomSeed(analogRead(A0)). - Tell "random" apart from "does not repeat".
What you need
| Part | Type | Qty |
|---|---|---|
| Arduino UNO R3 | Microcontroller | 1 |
| USB A to B cable | Cable | 1 |
Basic
No wiring. Read everything in the Serial Monitor at 9600 baud.
Two forms
| Call | Returns |
|---|---|
random(max) | 0 up to max − 1 |
random(min, max) | min up to max − 1 |
The top number is never returned. This catches everybody once.
random(6) // 0, 1, 2, 3, 4 or 5
random(1, 7) // 1, 2, 3, 4, 5 or 6 — a dice
random(10, 20) // 10 up to 19
For a dice you write random(1, 7), not random(1, 6). Say it out loud: up to but not including.
It repeats
void setup() {
Serial.begin(9600);
Serial.println(random(1, 7));
Serial.println(random(1, 7));
Serial.println(random(1, 7));
}
void loop() {
}
Upload it, note the three numbers, then press reset. The same three numbers. Every time, on every board of the same type.
That is not a fault. The generator is a formula, and it always starts from the same place.
Seeding it
void setup() {
Serial.begin(9600);
randomSeed(analogRead(A0));
Serial.println(random(1, 7));
Serial.println(random(1, 7));
Serial.println(random(1, 7));
}
void loop() {
}
analogRead(A0) on a pin with nothing connected returns a value that wanders with electrical noise. It is a poor sensor and an excellent seed.
Call randomSeed() once, in setup(). Seeding repeatedly — especially with the same value — makes the output worse, not better.
⚠️ If A0 has something wired to it, the reading is steady and the seed is the same every time. Use a pin you have genuinely left empty.
Random is not the same as unique
Roll a dice three times and you may get 4, 4, 4. Nothing is wrong.
Pupils often report that random() is "broken" because it repeated a number. Ask them how often they would expect a repeat from six possibilities in three rolls. It is far more likely than it feels.
Watch out for
- The upper bound is excluded.
random(1, 7)for a dice. random()returns along. Storing it in anintis fine for small ranges.- Seed once, in
setup(). - An unconnected analog pin is the seed — a connected one is not.
random(0)or a range of zero width returns 0 and looks broken.
Challenges
Challenge 1
A dice.
Print a dice roll once every second: a whole number from 1 to 6.
Run it for a minute. Did you ever see a 0? Did you ever see a 7? Did you see the same number twice in a row?
Then change it to random(1, 6) and run again. What is now missing, and why?
Log in to ask for the answer.
Challenge 2
Prove it repeats, then stop it.
- Print five random numbers in
setup()only. Write them down. - Press reset. Write them down again. Compare.
- Add
randomSeed(analogRead(A0))as the first line ofsetup(). - Reset several more times.
Write one sentence explaining what the seed changed, and one saying why an unconnected pin is a good source for it.
Log in to ask for the answer.
Challenge 3
Is it fair?
Roll a dice 60 times in setup() and print every result on one line.
Copy the output into a tally on paper: how many 1s, how many 2s, and so on.
With 60 rolls you would expect about 10 of each. Are you close? Would you expect to be exactly 10 of each? Write one sentence.
Log in to ask for the answer.
Extra challenge
A magic 8-ball.
Every two seconds, print one of six answers at random:
Yes
No
Maybe
Ask again later
Definitely
I would not
You do not have arrays yet, so use random(1, 7) and print a different line for each number. It will be repetitive — that is deliberate.
Think about it: count the lines your version takes. When arrays arrive in ARD-PG-12, the same six answers fit in one line and the printing fits in two. Keep this sketch and come back to it.
Log in to ask for the answer.