The shell generates a specified range of random numbers with random strings.

Source: Internet
Author: User

Shell generates a specified range of random numbers and random stringsCategory: Shell 2014-04-22 22:17 20902 People read Comments (5) favorite reports Shellrandomurandomuuidlinux

Shell generates a specified range of random numbers and random strings

1. Using the $RANDOM variables of the system

[Plain]View Plaincopyprint?
    1. [Email protected]:~$ Echo $RANDOM
    2. 17617
[Email protected]:~$ echo $RANDOM 17617
the range of $RANDOM is [0, 32767]

If you need to generate more than 32767 random numbers, you can do so in the following ways.

Example: Generating a random number of 400000~500000

[Plain]View Plaincopyprint?
    1. #!/bin/bash
    2. function rand () {
    3. Min=$1
    4. max=$ (($2-$min + 1))
    5. num=$ (($RANDOM +1000000000)) #增加一个10位的数再求余
    6. echo $ (($num% $max + $min))
    7. }
    8. rnd=$ (Rand 400000 500000)
    9. Echo $rnd
    10. Exit 0
#!/bin/bashfunction rand () {    min=$1    max=$ (($2-$min + 1))    num=$ (($RANDOM +1000000000)) #增加一个10位的数再求余    echo $ (($num% $max + $min))}rnd=$ (Rand 400000 500000) echo $rndexit 0

2. Using the date +%s%n

Example: Generating a random number of 1~50

[Plain]View Plaincopyprint?
    1. #!/bin/bash
    2. function rand () {
    3. Min=$1
    4. max=$ (($2-$min + 1))
    5. num=$ (Date +%s%n)
    6. echo $ (($num% $max + $min))
    7. }
    8. rnd=$ (Rand 1 50)
    9. Echo $rnd
    10. Exit 0
#!/bin/bashfunction rand () {    min=$1    max=$ (($2-$min + 1))    num=$ (date +%s%n)    echo $ (($num% $max + $min))} rnd=$ (Rand 1) echo $rndexit 0
3. Using/dev/random and/dev/urandom

The/dev/random stores the real-time data of the current operating environment of the system, which is a random number generator that is blocked and sometimes needs to wait for reading.

/dev/urandom non-blocking random number generator, the read operation does not cause blocking.

Example: Using/dev/urandom to generate 100~500 random numbers, use urandom to avoid blocking.

[Plain]View Plaincopyprint?
    1. #!/BIN/BASH  
    2.   
    3. function rand () {   
    4.     min=$1  
    5.      max=$ (($2-$min + 1))   
    6.     num=$ (cat /dev/urandom | head  -n 10 | cksum | awk -F  '   '   ' {print $1} '   
    7.     echo $ (($num% $max + $min))   
    8. }  
    9.   
    10. rnd=$ (rand 100 500)   
    11. echo   $rnd   
    12.   
    13. EXIT 0  
#!/bin/bashfunction rand () {    min=$1    max=$ (($2-$min + 1))    num=$ (cat/dev/urandom | head-n | cksum | awk-f " ' {print $} ')    echo $ (($num% $max + $min))}rnd=$ (rand) echo $rndexit 0
4. Using the Linux UUID

The UUID full name is a universal unique identifier, formatted with 32 16 binary digits, with a '-' connection number divided into 5 segments. The form is 32 characters of 8-4-4-4-12.

[Plain]View Plaincopyprint?
    1. [Email protected]:~/shell$ cat/proc/sys/kernel/random/uuid
    2. Fd496199-372a-403e-8ec9-bf4c52cbd9cd
[Email protected]:~/shell$ CAT/PROC/SYS/KERNEL/RANDOM/UUIDFD496199-372A-403E-8EC9-BF4C52CBD9CD
Example: generating 100~500 random numbers using Linux uuid

[Plain]View Plaincopyprint?
    1. #!/BIN/BASH  
    2.   
    3. function rand () {   
    4.     min=$1  
    5.      max=$ (($2-$min + 1))   
    6.     num=$ (cat /proc/sys/kernel/random/uuid  | cksum | awk -F  '   '   ' {print $1} '   
    7.     echo $ (($num% $max + $min))   
    8. }  
    9.   
    10. rnd=$ (rand 100 500)   
    11. echo  $rnd   
    12.   
    13. EXIT 0  
#!/bin/bashfunction rand () {    min=$1    max=$ (($2-$min + 1))    num=$ (Cat/proc/sys/kernel/random/uuid | cksum | Awk-f "{print $} ')    echo $ (($num% $max + $min))}rnd=$ (rand) echo $rndexit 0
5. Generating random strings

Example: Generating a 10-bit random string

[Plain]View Plaincopyprint?
    1. #使用date Generate random strings
    2. Date +%s%n | md5sum | Head-c 10
    3. #使用/dev/urandom Generate random strings
    4. Cat/dev/urandom | Head-n 10 | md5sum | Head-c 10

The shell generates a specified range of random numbers with random strings.

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.