DIY Random number generator

A  random number generator using 8051 that displays a random number between 0 & 99 is shown in this article. The circuit itself is very simple and may not find any applications in serious embedded projects and this article is just an illustration. The circuit is based on AT89S51 microcontroller, two seven segment LED displays, two transistors and few passive components.

Circuit diagram

random number generator using 8051
Random number generator using 8051

List of components

  • Microcontroller (AT89S51) – 1 nos
  • Push button switches – 2nos
  • 7-segment display – 2 nos
  • Transistor (2N2222) – 2 nos
  • Resistor (100 ohm) – 10 nos
  • Resistor (8.2k ohm) – 1 nos
  • Resistor (10k ohm) – 1 nos
  • Capacitor (10uF/10V) – 2 nos


The two seven segment LED displays are multiplexed together and their data lines are connected to Port0  of the microcontroller. Transistors Q1 and Q2 drive the corresponding displays D1 and D2. The driving signals for there transistors are obtained from P1.1 and P1.2. Push button switch S1, capacitor C1 and resistor R10 forms a debouncing reset circuit. Resistor R9, capacitor C2 and pushbutton switch S2 will provide an active low hardware interrupt signal at INTO (pin12) whenever S2 is pressed. Here also R9 and C2 are meant for debouncing.

After power ON the display will show blank and when push button S2 is pressed the display will show a random number between 0 and 99. For another try, you have to press the reset switch and then switch S2. If you need a single digit setup only, then remove the display D2 and its associated components. Everything else is the same.


ORG 000H
ORG 003H // sets the starting address for the ISR
ACALL ISR // calls the ISR subroutine when S2 is pressed
RETI // return from interrrupt

MAIN:SETB IP.0 // this part sets the initial conditions
     SETB TCON.0
     SETB IE.0
     SETB IE.7
     MOV P0,#00000000B
     MOV P1,#00000000B
     MOV DPTR,#LUT // moves the starting address of LUT to DPTR

LABEL:MOV R6,#99D // this part generates the random number
      LOOP:MOV A,R6
      DJNZ R6,LOOP

ISR: MOV A,R6 // Subroutine ISR displays the current random number
     MOV B,#10D
     DIV AB
     SETB P1.2
     MOV P0,A
     MOV A,B
     CLR P1.2
     SETB P1.1
     MOV P0,A
     CLR P1.1

DELAY: MOV R3,#02H // this subroutine creates 1mS delay for switching the displays
      DJNZ R3,DEL1

DISPLAY: MOVC A,@A+DPTR // produces the digit drive pattern for the current digit in A

LUT: DB 3FH // Look up table
     DB 06H
     DB 5BH
     DB 4FH
     DB 66H
     DB 6DH
     DB 7DH
     DB 07H
     DB 7FH
     DB 6FH

About the program

The first part of the program is the portion labelled MAIN which sets the initial conditions and the interrupt parameters. The next part is the loop named LABEL which loads 99D to register R6  then decrements it by 1 until 0 and then repeats the cycle again. This is the part which generates the random number. Every time R6 is decremented the resultant value is moved to accumulator A. Next part is the interrupt service routine which is written as a subroutine named ISR. Whenever there is an interrupt at INT0 (push button S2 is pressed), the ISR is called.

The ISR performs necessary mathematical manipulations on the content of A in order to split out the two digits and then proceeds to show it on the display. Subroutine DELAY produces roughly 1ms delay for switching the displays. Subroutine DISPLAY adds the current value in A with the address stored in DPTR (starting address of LUT) and moves the target content to A. The result will be the digit drive pattern for the current digit in A.



  1. Venkiteswaran T.K.

    Thanks a lot for the project for Microcontroller programming startups.

  2. avinash chavan

    can anyone post .hex file for this program?

  3. Pls tell me about the practical applications of random number generator


    can i know which display is used for this project reply fast plz…..
    e.g. common cathode or common anode display .


      aap ne kaise banaya h. plz meri help karo mere pass sirf 6 din h.

  5. Walemoloye

    Good job. I love this. Please I want to design this circuit for a 6-digit number display. How do I go about it?