The Method of Random Number Generation by Single-chip Timer

Source: Date: 2020-11-25

Single-chip microcomputers are widely used in various fields. There are many ways to use single-chip microcomputers to generate random numbers. One of them is to use single-chip timers to take out the unknown timer THX and TLX values, and then perform operations to obtain a random value within a specified range. This approach is also feasible. Or write a random number table in advance, and then get the data. it is also fine.

The Method of Random Number Generation by Single-chip Timer

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 realize random number generation. There are three general random number generators in the C language, namely the rand function, the random function, and the randomize function; but what the rand function generates is not a random number that is true and justice. It is a pseudo-random number, which is called according to a number. It is a coefficient calculated by a recursive formula as a reference. When the number of this series is large, it conforms to the normal announcement, which is equivalent to generating a random number, but this is not a true random number. After the normal boot, the value of this seed is fixed, unless the system is destroyed, in order to change the value of this seed, C provides the srand() function, its original shape is void srand( int a); when calling the rand function to generate random Before counting, 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. Generally use the for statement to set the number of seeds.
 Two Methods of Random Number Generation by Single-chip Timer


Method 1: Single-chip timer directly randomly selects a value
Every time you press the button, a random number is generated. This random number actually takes the value of the timer out. It cannot be regarded as absolute randomness. Method 2 is truly random.


 Method 2: Use the single-chip timer and rand() random function to achieve
After the single-chip microcomputer is powered on, press the key to start the random number. If the single-chip microcomputer immediately takes the random number when it is powered on, the random result is the same every time it is powered on. Then there is a non-repeating random number from 0 to 9. The program uses a loop to determine whether the random number is the same as the previous random number. If it is the same, enter it. Next time, the random number is taken, and the difference is stored in the array.

To

Development strength

Professional and experienced software and hardware

Global channels

Provide customers with cost-effective products and services

Quality Assurance

The professional test guarantee team guarantees the stability

After-sales service

Professional technical service team, 24 hours to provide customers