How to generate random numbers in Linux system

王林
Release: 2020-06-29 10:32:23
Original
5804 people have browsed it

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
Copy after login

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
Copy after login

Method 2: Generate random numbers through openssl

Example:

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

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
Copy after login

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
Copy after login

/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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template