Build it step by step
0 of 7 doneWire the button between D2 and GND
Leave A0 unconnected.
Upload and open the serial monitor at 9600
Tests A and B print at once.
Press the button when asked
Test C prints.
Note the first five of each test
Write them down.
Press reset and repeat several times
Compare the lists.
Note which tests repeat
A always repeats.
Compare the bar charts
All three look fairly even.
How do you build a random number quality test?
Draw thousands of random numbers from a Nano with a fixed seed, a floating-pin seed and a timing seed, count how often each value comes up and print a bar chart. You see which kinds of randomness repeat and which do not. You need Arduino Nano, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 7 steps and about 60 minutes.
- Level
- Intermediate
- Time
- About 60 min
- Parts
- 4
Parts list
- Development BoardsBuyArduino Nano × 1
- SwitchesBuy
- PrototypingBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Button leg 1 | Nano D2 | Used to make a timing seed |
| Button leg 2 | Nano GND | Pressing joins D2 to ground |
| Nano A0 | Left unconnected | A floating pin used for the second seed |
Code
A computer's random numbers come from a formula that starts from a seed, so the same seed always gives the same list. The sketch draws five thousand numbers from zero to nine three times, each time with a different kind of seed, and prints how often each value came up, with a bar for each. Each count should be near 500, and the biggest gap from 500 tells you how even the numbers are. The more important test is to press the Nano's reset button several times and compare the first five numbers of each test. A, the fixed seed, gives the same five every time. B, the floating analog pin, gives a few different lists, but not very many, because the pin tends to read similar values. C, the microsecond clock at the moment you press the button, gives a different list almost every time. This is good enough for games and dice but is never suitable for passwords or security.
Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
const uint8_t BUTTON_PIN = 2;
const int DRAWS = 5000;
const int BINS = 10;
void report(const char* name) {
int counts[BINS] = {0};
Serial.print(name);
Serial.print(" first five: ");
for (int i = 0; i < DRAWS; i++) {
long v = random(BINS); // 0 to 9
counts[v]++;
if (i < 5) { Serial.print(v); Serial.print(' '); }
}
Serial.println();
int worst = 0;
for (int b = 0; b < BINS; b++) {
Serial.print(b);
Serial.print(" ");
Serial.print(counts[b]);
Serial.print(" ");
for (int i = 0; i < counts[b] / 25; i++) Serial.print('#');
Serial.println();
int dev = abs(counts[b] - DRAWS / BINS);
if (dev > worst) worst = dev;
}
Serial.print("Biggest gap from the expected 500: ");
Serial.println(worst);
Serial.println();
}
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
Serial.begin(9600);
Serial.println("Press reset several times and compare the 'first five' lines.");
Serial.println();
randomSeed(1); // A: a fixed seed
report("A fixed seed ");
randomSeed(analogRead(A0)); // B: a floating pin
report("B floating pin seed");
Serial.println("Press the button for test C (timing seed)...");
while (digitalRead(BUTTON_PIN) == HIGH) {}
randomSeed(micros()); // C: microseconds at the moment of the press
report("C timing seed ");
}
void loop() {}What the serial monitor should show
Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Nano with its sensors at their default values. Your numbers will follow your real readings, but the format should match.
Press reset several times and compare the 'first five' lines. A fixed seed first five: 6 0 5 9 9 0 510 #################### 1 506 #################### 2 469 ################## 3 521 ####################
Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Test A after each reset | The same first five numbers |
| Test B after resets | A few different lists, with repeats |
| Test C after resets | Almost always different |
| Counts per value | Each near 500 |
| Biggest gap | Usually under 60 |
If it does not work
Nothing prints
Set 9600 baud in the monitor.
Test C never runs
Press the button once.
B looks the same every time
A floating pin is a weak seed, which is the lesson.
Bars look uneven
Run it again, some variation is normal.
Upload fails
Choose the Old Bootloader option.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the random number quality test?
Arduino Nano, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).
How long does the random number quality test take to build?
About 60 minutes for a intermediate maker, in 7 steps.
How do I know the random number quality test works?
Test A after each reset: The same first five numbers. Test B after resets: A few different lists, with repeats. Test C after resets: Almost always different.
Nothing prints: what should I check?
Set 9600 baud in the monitor.
Test C never runs: what should I check?
Press the button once.
B looks the same every time: what should I check?
A floating pin is a weak seed, which is the lesson.
Bars look uneven: what should I check?
Run it again, some variation is normal.
