Ways to get random numbers from Linux

Source: Internet
Author: User
Tags base64 base64 encode openssl uuid

The method of 1.1.1 Inux random number http://www.2cto.com/kf/201410/342717.html

Method one. [[Email protected] ~]# date +%n%N nanoseconds randomly acquired nine-digit pass time

823015723

Get random numbers by time (date)

Date +%s%n #生成19位数字, 1287764807051101270

Date +%s%n | Cut-c6-13 #取八位数字, 21793709

Date +%s%n | md5sum | Head-c 8 #八位字母和数字的组合, 87022FDA

Method Two. [Email protected] ~]# echo $RANDOM

24215

Generates an integer random number between 0-32767, and if more than 5 bits can be added to a fixed 10-bit integer, then the remainder is obtained.

Generate a random number of 400000~500000:

#!/bin/bash

function rand () {

Min=$1

max=$ (($2-$min + 1))

num=$ (($RANDOM +1000000000)) #增加一个10位的数再求余

echo $ (($num% $max + $min))

}

rnd=$ (Rand 400000 500000)

Echo $rnd

Exit 0

Method Three: Using Awk's random function

awk ' Begin{srand ();p rint rand () *1000000} ' #可以加上if判断, 779644

Method Four OpenSSL rand generates random numbers

OpenSSL Rand is used to produce random characters of a specified length of bytes. -base64 or-hex base64 encode random strings or display them in hex format.

OpenSSL Rand-base64 8 | md5sum | Cut-c1-8 #八位字母和数字的组合, 3a61800e

OpenSSL Rand-base64 8 | Cksum | Cut-c1-8 #八位数字, 10784736

Method Five

(5) Generate random numbers (/dev/random and/dev/urandom) from unique data within the system

The real-time data of the environment currently running on the/dev/random storage system can be regarded as a unique value data at some time of the system, providing high quality random numbers.

/dev/urandom is a non-blocking random number generator that does not generate blocking, faster, and less secure random number generators when read.

Cat/dev/urandom | Head-n 10 | md5sum | Head-c #32f1e953ac

Cat/dev/urandom | Strings-n 8 | Head-n 1 #生成全字符的随机字符串, 08? Wu$zu

Cat/dev/urandom | Sed-e ' s/[^a-za-z0-9]//g ' | Strings-n 8 | Head-n 1 #生成数字加字母的随机字符串, Ql2q9cxs

Where Strings-n sets the number of characters in the string, Head-n sets the number of rows to output.

Head-200/dev/urandom| Cksum |cut-d ""-f1 #urandom的数据很多使用cat会比较慢, using head to read 200 lines, Cksum will read the contents of the file to generate a unique representation of the integer data, cut to "" split and then get the first field data split

(6) Read the Linux UUID code

The UUID code is the universal unique identifier (universally unique Identifier, UUID), the UUID format is: Contains 32 16 binary digits, with "-" connection number is divided into five segments, in the form of 32 characters 8-4-4-4-12. The UUID code of Linux is also provided by the kernel, in/proc/sys/kernel/random/uuid this file. Cat/proc/sys/kernel/random/uuid data will be different each time it is acquired.

cat/proc/sys/kernel/random/uuid| Cksum | Cut-f1-d "" #获取不同的随机整数, 1675034933

cat/proc/sys/kernel/random/uuid| md5sum | Cut-c1-8 #数字加字母的随机数, D69A7EBF

Generate 100~500 random numbers using the Linux UUID:

#!/bin/bash

function rand () {

Min=$1

max=$ (($2-$min + 1))

num=$ (Cat/proc/sys/kernel/random/uuid | cksum | awk-f "' {print $} ')

echo $ (($num% $max + $min))

}

rnd=$ (Rand 100 500)

Echo $rnd

Exit 0

(7) Random extraction from the element pool

Pool= (a b c d e F g h i j k l m n o p q R S t 1 2 3 4 5 6 7 8 9 10)

num=${#pool [*]}

result=${pool[$ ((random%num))}

61b53abd4e1eee16cb7966ccb2aa89da-

[[Email protected] ~]# date +%n|md5sum|cut-c 6-13

E2d802fb

[[email protected] ~]# echo ' Date +%n|md5sum|cut-c 6-13 '

8ba21fee

[[email protected] ~]# echo ' date +%n|md5sum|cut-c 6-13 ' |passwd stud{1..8}^c

[[email protected] ~]# Useradd std{1..5}

232 randomly created passwords

Ways to get random numbers from Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.