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?
- [Email protected]:~$ Echo $RANDOM
- 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?
- #!/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
#!/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?
- #!/bin/bash
- function rand () {
- Min=$1
- max=$ (($2-$min + 1))
- num=$ (Date +%s%n)
- echo $ (($num% $max + $min))
- }
- rnd=$ (Rand 1 50)
- Echo $rnd
- 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?
- #!/BIN/BASH  
-
- function rand () {
- min=$1
- max=$ (($2-$min + 1))
- num=$ (cat /dev/urandom | head -n 10 | cksum | awk -F ' ' ' {print $1} '
- echo $ (($num% $max + $min))
- }
-   
- rnd=$ (rand 100 500)
- echo $rnd
-
- 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?
- [Email protected]:~/shell$ cat/proc/sys/kernel/random/uuid
- 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?
- #!/BIN/BASH  
-
- function rand () {
- min=$1
- max=$ (($2-$min + 1))
- num=$ (cat /proc/sys/kernel/random/uuid | cksum | awk -F ' ' ' {print $1} '
- echo $ (($num% $max + $min))
- }
-   
- rnd=$ (rand 100 500)
- echo $rnd
-
- 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?
- #使用date Generate random strings
- Date +%s%n | md5sum | Head-c 10
- #使用/dev/urandom Generate random strings
- Cat/dev/urandom | Head-n 10 | md5sum | Head-c 10
The shell generates a specified range of random numbers with random strings.