▷ Generate pseudo-random numbers - PIC/Microcode

In this post, we will see how to generate pseudo-random numbers and print them on a 16x2 LCD using the PIC 16F628A microcontroller.

WHAT IS A PSEUDO-RANDOM NUMBER?

A pseudo-random number is a number generated in a process that seems to produce random numbers, but does not actually do so. Pseudo-random number sequences do not show any apparent pattern or regularity from a statistical point of view, despite being generated by a completely deterministic algorithm, where the same initial conditions always produce the same result.

MATERIALS:

The materials used for this project are as follows:

  • PIC 16F628A
  • LCD 16*2

WIRING DIAGRAM:

OPERATION:

When powering the microcontroller, random numbers will appear on the LCD; in this case, the numbers that will appear will range from 0 to 50. If we want to obtain values greater than 50, we must modify the following values.

X = (((7*X)+5) Mod 99)
N = X Mod 51

Example:

If we want to generate random numbers with values ranging from 0 to 100, we modify:

X = (((7*X)+5) Mod 199)
N = X Mod 101

For the generation of pseudo-random numbers, we base ourselves on the following algorithm:Linear congruential generator

CODE:

'****************************************************************
'*  Name    : UNTITLED.BAS                                      *
'*  Author  : Esteban Carrillo - www.edeptec.com                *
'*  Notice  : Copyright (c) 2022                                *    
'*          : All Rights Reserved                               *
'*  Date    : 18/03/2021                                        *
'*  Version : 1.0                                               *
'*  Notes   :                                                   *
'*          :                                                   *
'****************************************************************
DEFINE LCD_DREG PORTB 
DEFINE LCD_DBIT 0 
DEFINE LCD_RSREG PORTB 
DEFINE LCD_RSBIT 4 
DEFINE LCD_EREG PORTB 
DEFINE LCD_EBIT 5

x VAR BYTE
y VAR BYTE
n VAR BYTE

y = 5
CICLO: 
    x = (((7*x)+5) Mod 99)
    n = x Mod 50

    LCDOUT $fe, 1
    LCDOUT $fe, $80 + y, "Num:" ,DEC n
    PAUSE 500
GOTO CICLO

0/Leave a comment/Comments

Hello! We're so glad you've made it this far and are reading this article on Edeptec.

This form is an open space for you: you can leave a comment with your questions, suggestions, experiences, or simply your opinion on the topic discussed.

» Did you find the information helpful?
» Do you have any personal experiences you'd like to share?
» Do you have any topics you'd like to see covered in future articles?

Remember that this space is for learning and sharing, so we encourage you to participate respectfully and constructively. Your comments can help other readers who are on the same path, whether in electronics, programming, sports, or technology.

Thank you for being part of this learning community! Your participation is what makes this project grow.