TUTORIALS

How to Build a Random Number Generator [with Code] | Shortcuit

This tutorial will show you how to build a Random Number Generator Arduino project. This project uses multiplexed 7 segment displays and the random() function.

21 Feb 2022

In this post, you'll learn how to build a beginner Arduino project with multiplexed 7 segment displays - the Random Number Generator project.

Project Info

The 7 segment displays start with 3 zeros and when you press on the pushbutton, it generates a random number between 0 and 999.

Video Tutorial

You can watch the video tutorial where I build the project step-by-step.

Which components do you need?

Virtual components in Shortcuit: breadboard, Arduino UNO (Shortcuit UNO); 3 x 7 segment displays, 7 x 200ohm resistors and pushbutton
  • 3 x 7 segment displays (common cathode)
  • 7 x 220Ω resistor ( > 160Ω)
  • 1 x pushbutton
  • 1 x breadboard
  • 1 x Arduino Uno (Shortcuit Uno)
  • jumper wires

How to build the project?

Virtual breadboard with 7 resistors and wires from displays to resistors in top view

1. Multiplex 7 segment displays

Place the 3 7 segment displays in the breadboard.

To reduce the number of digital pins needed to use the 3 displays, we can multiplex them. First we need to connect the top segments of the first display to the 2nd display. Then we do the same from the 2nd display to the 3rd.  

Now connect the bottom segments of 1st display to 2nd and 2nd to 3rd.

2. Place and connect resistors

To make sure the LEDs of the displays won’t break, we should add resistors before connecting them to the digital pins. Use resistors with a value higher than 160Ω.

Connect the segments of the 1st display to the resistors.

From the other end of the resistors, we can draw wires to the digital pins.

3. Place and connect pushbutton

Place the pushbutton on the right side of the breadboard and connect it to Digital Pin 9 and the Ground.

4. Connect common cathode pins

Random Number Generator project with common cathodes of displays connected

As the last step, connect the common cathode pins to the digital pins. Move the wires more to the side so that it won’t cover the displays.

5. Add battery as power source (optional)

You can also add a battery to power the Arduino Uno, but you could just use the USB cable as well.

Code Overview

Now that all the components and wires are connected, we can paste the code in a new Arduino sketch file and upload it. Check out the YouTube tutorial for a more detailed explanation of the code.

/*
  Random Number Generator

  The 7 segment displays start with 3 zeros and when you press on the pushbutton, it generates a random number between 0 and 999.

  Created 21/02/2022
  By Shortcuit

  More information can be found in the blog post:
  >>> https://shortcuit.com/blog/tutorials/electronics-projects/how-to-build-a-random-number-generator-with-code/

  shortcuit.com
  Educational game to learn electronics and Arduino programming.
*/


// using 3 common cathode displays

int a = 2;
int b = 3;
int c = 4;
int d = 5;
int e = 6;
int f = 7;
int g = 8;
int cathode1 = 10;
int cathode2 = 11;
int cathode3 = 12;
int button = 9;

int randomDigit1 = 0;
int randomDigit2 = 0;
int randomDigit3 = 0;

const int SHOW_TIME = 2; //ms, the time before switching to the next display

void setup()
{
  Serial.begin(9600);
  
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  pinMode(f, OUTPUT);
  pinMode(g, OUTPUT);

  pinMode(cathode1, OUTPUT);
  pinMode(cathode2, OUTPUT);
  pinMode(cathode3, OUTPUT);
  
  pinMode(button, INPUT_PULLUP);
  
  // sets all cathode pins high so nothing is displayed
  digitalWrite(cathode1, HIGH);
  digitalWrite(cathode2, HIGH);
  digitalWrite(cathode3, HIGH);
  
  // gets a random seed by reading the noise at A0 so random is unique every time
  randomSeed(analogRead(A0));
}

void loop()
{
  // continuously multiplex the displays and show the current random digits
  Write3DigitNumber(randomDigit1, randomDigit2, randomDigit3);
  
  // if the button is pressed create new random digits
  if(digitalRead(button) == LOW)
  {
    randomDigit1 = random(0, 10);
    randomDigit2 = random(0, 10);
    randomDigit3 = random(0, 10);
  }
}

// Multiplexes the digits one by one
void Write3DigitNumber(int digit1, int digit2, int digit3)
{
  // show first digit on first display
  WriteDigitToSpecificDisplay(digit1, cathode1); 
  // show second digit on second display
  WriteDigitToSpecificDisplay(digit2, cathode2); 
  // show third digit on third display
  WriteDigitToSpecificDisplay(digit3, cathode3); 
}

// Writes the digit to a specific display for a short moment (DELAY_TIME)
void WriteDigitToSpecificDisplay(int digit, int cathodeDisplay)
{
  WriteDigitToSegments(digit); // set the segments high
  digitalWrite(cathodeDisplay, LOW); // only set the cathode of the display we wanna show to LOW
  delay(SHOW_TIME); // show the number for SHOW_TIME milliseconds 
  digitalWrite(cathodeDisplay, HIGH); // set the cathode HIGH again (so nothing is showing)
}

// Sets the segments high/low for a specific digit
void WriteDigitToSegments(int digit) // value from 0-9
{
  switch(digit)
  {
    case 0:
     SetAllSegmentPinsAtOnce(HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW);
     break;
    case 1:
     SetAllSegmentPinsAtOnce(LOW, HIGH, HIGH, LOW, LOW, LOW, LOW);
     break;
    case 2:
     SetAllSegmentPinsAtOnce(HIGH, HIGH, LOW, HIGH, HIGH, LOW, HIGH);
     break;
    case 3:
     SetAllSegmentPinsAtOnce(HIGH, HIGH, HIGH, HIGH, LOW, LOW, HIGH);
     break;
    case 4:
     SetAllSegmentPinsAtOnce(LOW, HIGH, HIGH, LOW, LOW, HIGH, HIGH);
     break;
    case 5:
     SetAllSegmentPinsAtOnce(HIGH, LOW, HIGH, HIGH, LOW, HIGH, HIGH);
     break;
    case 6:
     SetAllSegmentPinsAtOnce(HIGH, LOW, HIGH, HIGH, HIGH, HIGH, HIGH);
     break;
    case 7:
     SetAllSegmentPinsAtOnce(HIGH, HIGH, HIGH, LOW, LOW, LOW, LOW);
     break;
    case 8:
     SetAllSegmentPinsAtOnce(HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH);
     break;
    case 9:
     SetAllSegmentPinsAtOnce(HIGH, HIGH, HIGH, HIGH, LOW, HIGH, HIGH);
     break;
    default:
     Serial.println("A value from 0 to 9 was expected!");
     break;
  }
}

// Function that makes it easy to set all the states of the segment pins in one line of code.
void SetAllSegmentPinsAtOnce(bool segmentA, bool segmentB, bool segmentC, bool segmentD, bool segmentE, bool segmentF, bool segmentG) 
{
  digitalWrite(a, segmentA);
  digitalWrite(b, segmentB);
  digitalWrite(c, segmentC);
  digitalWrite(d, segmentD);
  digitalWrite(e, segmentE);
  digitalWrite(f, segmentF);
  digitalWrite(g, segmentG);
}

Final Result

Shortcuit Project

Random number generator Arduino project in Shortcuit

Real Project

Random number generator project with real components

Get on early access list

Be one of the first to try it when early release launches.