Pick A Number From 1 To 4: Fairness Test Explained

Last Updated: Written by Aaron J. Whitmore
pick a number from 1 to 4 fairness test explained
pick a number from 1 to 4 fairness test explained
Table of Contents

Pick a Number From 1 to 4

The correct microcontroller code is to use a random function that returns a value from 1 through 4, such as random(1, 5) in Arduino-style code, because the upper limit is exclusive. If you are using an ESP32, you can also generate a bounded random value from its hardware RNG and map it into the same 1-to-4 range.

How It Works

In Arduino, random(min, max) returns a pseudo-random integer where min is included and max is excluded, so random(1, 5) can produce 1, 2, 3, or 4. The Arduino ecosystem commonly improves randomness by calling randomSeed() with noise from an unused analog pin, which helps vary the sequence after each reset.

pick a number from 1 to 4 fairness test explained
pick a number from 1 to 4 fairness test explained

On ESP32 boards, the system can use a hardware random number generator through esp_random(), and the documentation states that it can provide true random numbers when Wi-Fi or Bluetooth is enabled. For a classroom project, that means you can teach both the concept of pseudo-randomness on Arduino and hardware-backed randomness on ESP32.

Example Code

Use this Arduino sketch to pick one number from 1 to 4 and print it to the Serial Monitor. The logic is simple, beginner-friendly, and matches the standard Arduino random range behavior.

void setup() {
 Serial.begin;
 randomSeed(analogRead(A0)); // optional if A0 is floating
}

void loop() {
 int choice = random; // returns 1, 2, 3, or 4
 Serial.println(choice);
 delay;
}

If you want the result to appear only once after boot, place the random call inside setup() instead of loop(). That is often the better pattern for a single choice project, such as a classroom selector, game spinner, or robotics decision step.

Build Notes

  • Arduino UNO: Use random(1, 5) for the number and randomSeed(analogRead(A0)) for a better starting point.
  • ESP32: Use the built-in RNG when you need stronger randomness behavior than a basic seeded sequence.
  • Range rule: The upper bound is excluded, so random(1, 5) is the correct way to include 4.
  • Educational use: This pattern is useful for dice-like decisions, robot branching logic, and simple interactive electronics.

Quick Reference

Platform Best method Result range
Arduino UNO random(1, 5) with optional seed 1 to 4
ESP32 esp_random() then bound the output 1 to 4
Any microcontroller Seeded pseudo-random generator 1 to 4

Step-by-Step Use

  1. Connect your microcontroller and open the code editor.
  2. Initialize Serial so you can view the output.
  3. Add a random seed if your board supports it and you want less repetition.
  4. Call the random function with the range 1 to 5.
  5. Print or use the chosen number in your circuit logic.

Common Questions

Teaching Value

This small project reinforces a core programming idea: a bounded random output is often more useful than an unrestricted one. It also gives students a practical way to connect coding logic with real hardware behavior, which is exactly why microcontroller exercises work so well in STEM classrooms.

"Use random(1, 5) when you want a microcontroller to pick one number from 1 to 4."

Expert answers to Pick A Number From 1 To 4 Fairness Test Explained queries

Why is the upper number 5?

Because Arduino-style random functions use an inclusive lower limit and an exclusive upper limit, so 5 must be used to allow 4 as a possible output.

Is this truly random?

On Arduino, the result is usually pseudo-random unless you seed it well, while ESP32 can use hardware randomness under documented conditions.

Can I make it choose only once?

Yes, generate the number inside setup() and store it in a variable so the board keeps one choice until reset.

Explore More Similar Topics
Average reader rating: 4.9/5 (based on 139 verified internal reviews).
A
Tech Education Correspondent

Aaron J. Whitmore

Aaron J. Whitmore is a technology education correspondent with a background in electrical engineering and journalism. He earned a B.S. in Electrical Engineering from MIT and a Master's in Journalism from the Columbia University Graduate School of Journalism.

View Full Profile