Home  >  Article  >  Operation and Maintenance  >  How to generate random numbers in Linux system

How to generate random numbers in Linux system

王林
王林Original
2020-06-29 10:32:235740browse

The method for the Linux system to generate random numbers is: it can be achieved through the system environment variable [$RANDOM], such as [echo $RANDOM]. RANDOM has a limited range of random numbers, which can be solved by adding an encrypted string after the random number.

How to generate random numbers in Linux system

There are many ways to generate random numbers in the Linux system. Here are a few:

(Recommended learning: linux tutorial)

Method 1: Implemented through the system environment variable ($RANDOM)

[root@test ~]# echo $RANDOM
11595
[root@test ~]# echo $RANDOM
21625

RANDOM’s random number range is 0~32767, so the encryption is not very good . It can be solved by adding an encrypted string (that is, a string related to password generation) after the random number, and finally performing the md5sum operation together and intercepting the last n digits of the result. In this way, it cannot be based on the random range 0~32767 Guess the specific result.

Example:

[root@test ~]# echo "$RANDOM"|md5sum|cut -c 5-15
4eaf70019cc

Method 2: Generate random numbers through openssl

Example:

[root@test ~]# openssl rand -base64 8
yB0maNWRoQw=

Let the number be By combining uppercase and lowercase characters and adding special characters, a long number of digits can be achieved. Such random numbers are very safe.

Method 3: Get a random number through time (date)

Example:

[root@test ~]# date +%s%N
1523402619479946400
[root@test ~]# date +%s%N
1523402622015235600

Method 4: Cooperate through /dev/urandom chksum generates random numbers

[root@test ~]# head /dev/urandom|cksum
2866845253 2890
[root@test ~]# head /dev/urandom|cksum
2131526544 2440

/dev/random device stores real-time data of the current operating environment of the system.

It can be seen as the unique value of the system at a certain time, so it can be used as random metadata. We can read the data inside by reading the file. The data of the device /dev/urandom is the same as that of random. However, it is a non-blocking random number generator, and the read operation will not cause blocking.

The above is the detailed content of How to generate random numbers in Linux system. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn