How to generate random numbers using a single chip microcomputer

Publisher:MagicalSerenadeLatest update time:2020-06-04 Source: elecfansKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

There are many random numbers in the application of single-chip microcomputers. Of course, there are many ways to generate random numbers. One of them is to use the single-chip microcomputer timer to take out the unknown timer THX and TLX values, and then calculate to get a random value within a specified range. This approach is also feasible. Or you can write a random number table in advance and then take the data. This is also possible.

How to generate random numbers using a single chip microcomputer

The function that generates random numbers in KEIL is indeed rand(), but the header file is stdlib.h, not time.h. C language provides some library functions to generate random numbers. There are three common random number generators in C language, namely rand function, random function, and randomize function; however, the rand function does not generate a true random number, but a pseudo-random number, which is a coefficient calculated based on a number, called a seed, and a recursive formula. When this series of numbers is very large, it conforms to the normal distribution, which is equivalent to generating random numbers, but this is not a true random number. When the computer is turned on normally, the value of this seed is fixed, unless the system is damaged. In order to change the value of this seed, C provides the srand() function, whose prototype is void srand(int a); before calling the rand function to generate random numbers, you must first use srand() to set the random number seed. If the random number seed is not set, rand() will automatically set the random number seed to 1 when it is called. Generally, the for statement is used to set the number of seeds.


Two methods of generating random numbers using single chip microcomputer


Method 1: Timer directly takes random values

Each time a button is pressed, a random number is generated. This random number actually takes out the value of the timer and cannot be considered absolutely random. Method 2 is the real random number.


Method 2: Use a timer plus the rand() random function to implement

After the microcontroller is powered on, a button is pressed to start the random number fetching. If the microcontroller fetches random numbers immediately after powering on, the random number fetching result will be the same each time it is powered on. Then a non-repeating random number from 0 to 9 is generated. The program uses a loop to determine whether it is the same as the previously fetched random number. If it is the same, the program will enter and fetch the random number next time. If it is different, the program will store it in the array.

Keywords:MCU Reference address:How to generate random numbers using a single chip microcomputer

Previous article:Detailed analysis of CY and OV in MCU
Next article:Design of Flowing Light Using Single Chip Microcomputer Dot Matrix LED Light

Recommended ReadingLatest update time:2024-11-25 04:27

PIC Microcontroller Introduction_Frame and Memory
1.PIC16F877A microcontroller framework 1.1 Why is the performance so high? The reason why PIC microcontroller has high performance is that it has the following characteristics: The internal structure is Harvard, register files, most single instruction cycles, instruction pipeline operation, long word instructions,
[Microcontroller]
Design of multifunctional counter based on FPGA and single chip microcomputer
  1. System Solution   Solution 1: Use small and medium-sized digital circuits to form the system, with counters as the main measurement module and timers as the main control circuit. This solution has simple software design, but too many peripheral chips and narrow frequency band, making it more complicated to impl
[Microcontroller]
Let you use 51 microcontroller to make a simple electronic clock
I. Introduction Today I would like to recommend a small experiment with 51 microcontroller to you, using the 51 microcontroller to make a simple electronic clock. The timing module uses the DS1302 hardware module, and the display uses an LCD display. Let’s take a look at how to implement it! 2. Introduction to DS1
[Microcontroller]
Let you use 51 microcontroller to make a simple electronic clock
STM32 MCU PWM output test
environment: Host: XP Development environment: MDK4.23 MCU:STM32F103CBT6   illustrate: Use the internal 8M crystal oscillator, multiply it to 64M to supply the TIM3 timer, and generate 640K, 50% square wave on PA6 (channel 1) source code: Initialize the clock: //Initialize RCC clock   void init_rcc(void)   {  
[Microcontroller]
STM32 MCU PWM output test
MCU waveform generator source code
Program source code #include reg51.h #include intrins.h #include math.h #define  uchar  unsigned  char #define  uint  unsigned   int unsigned long Result,i; sbit SDA=P1^1; //PCF8591  interface sbit SCL=P1^0; unsigned int a=0; // waveform sampling point value unsigned int b=0; unsigned  int c=0; unsigned  int bx_
[Microcontroller]
MCU waveform generator source code
Research on LED display drive circuit in single chip computer system
In recent years, the single-chip microcomputer system has gradually penetrated into the practical engineering applications of various industries with its advantages of small size, powerful functions, flexible expansion and easy use. The LED display circuit is like the eyes of the single-chip microcomputer system, wh
[Microcontroller]
Research on LED display drive circuit in single chip computer system
Tips for 51 MCU register configuration
Tips for converting integer data to binary data: This technique is more suitable for the configuration of 51 microcontroller registers; it reduces our calculation workload. Idea - We are more accustomed to direct integer data; when we need to configure registers, can we directly use the data as the data for our c
[Microcontroller]
Tips for 51 MCU register configuration
Design of Computer Monitor Based on EM78P468 Single Chip Microcomputer
introduction As computer technology gradually penetrates into all aspects of social life, more and more people are using computers. Among these people, a large number are not familiar with computer hardware or do not know how to maintain it. Here, a computer monitor is designed. Its function is to timely re
[Microcontroller]
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号