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.
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.
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
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- CATL releases October battle report
- Battery industry in October 2024: growth momentum remains unabated!
- Mercedes-Benz will launch the eCitaro equipped with NMC4 batteries to provide high energy density and long life
- Many companies have announced progress on solid-state batteries. When will solid-state batteries go into mass production?
- Xsens Sirius Series Inertial Sensors Enable 3D Inertial Navigation in Harsh Environments
- Infineon's Automotive Landscape: From Hardware to Systems
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- [Synopsys IP Resources] How IP-based SoC design drives dynamic and diverse HPC demand to achieve full growth
- Video interference
- Analysis of the top ten popular microprocessor and memory new products in 20061
- CCS5.5&DSP/BIOS setup steps
- TI live broadcast at 8pm tonight: the latest DLP products and automotive application solutions are here, interpreting dynamic ground lights and AR-HUD solutions
- How to draw components on the back side of the PCB board?
- C2000 target board hardware troubleshooting steps
- Digital power is the research and development direction of power management chips
- Car "nuclear batteries" are coming? Lifespan up to 90 years without charging, is it a gimmick or a fact?
- Simulate serial port infrared transmitter