Shell Scripting Lesson Three: Generating random numbers

Source: Internet
Author: User

Generating random numbers in Linux can use the $random variable.

The Man Bash command looks at the explanation of the random.


Random each of the parameter is referenced, a random integer between 0 and 32767 is generated.


As you can see, the $RANDOM generates random numbers ranging from 0 to 32767.


If you want to generate a random number from 0 to 9, you can take a 10 modulo operation so that you can get a random number from 0 to 9.

[[email protected] ~]# echo $ (($RANDOM%10)) 8[[email protected] ~]# echo $ (($RANDOM%10)) 6[[email protected] ~]# echo $ (($RA NDOM%10)) 2[[email protected] ~]# echo $ (($RANDOM%10)) 0[[email protected] ~]# echo $ (($RANDOM%10)) 2[[email protected] ~]# echo $ ($RANDOM%10) 9[[email protected] ~]# echo $ (($RANDOM%10)) 6[[email protected] ~]# echo $ (($RANDOM%10)) 5


Randomly generate random numbers and verification codes from 1 to 10 bits

[[email protected] ~]# echo $ (($RANDOM)) | md5sum | Cut-c 1-105c55ba28b1[[email protected] ~]# echo $ (($RANDOM)) | md5sum | Cut-c 1-10fd6d39d9c1[[email protected] ~]# echo $ (($RANDOM)) | md5sum | Cut-c 1-102bfb089c18[[email protected] ~]# echo $ (($RANDOM)) | md5sum | Cut-c 1-1008089F9BE1

Use Md5sum to parse generated random numbers, and use Cut-c to split by character


You can use ${#var} When you are seeking the length of a variable.

[Email protected] ~]# A=abcdef[[email protected] ~]# echo ${#a}6

This article is from the "ssspure" blog, make sure to keep this source http://ssspure.blog.51cto.com/8624394/1862378

Shell Scripting Lesson Three: Generating random numbers

Related Article

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.