shell 產生指定範圍隨機數與隨機字串 .

來源:互聯網
上載者:User

標籤:

        shell 產生指定範圍隨機數與隨機字串                      分類:             shell              2014-04-22 22:17     20902人閱讀     評論(5)     收藏     舉報     shellrandomurandomuuidlinux

shell 產生指定範圍隨機數與隨機字串

 

1.使用系統的 $RANDOM 變數

 

[plain] view plaincopyprint?
  1. [email protected]:~$ echo $RANDOM  
  2. 17617  
[email protected]:~$ echo $RANDOM17617
$RANDOM 的範圍是 [0, 32767]

 

如需要產生超過32767的隨機數,可以用以下方法實現。

例:產生400000~500000的隨機數

 

[plain] view plaincopyprint?
  1. #!/bin/bash  
  2.   
  3. function rand(){  
  4.     min=$1  
  5.     max=$(($2-$min+1))  
  6.     num=$(($RANDOM+1000000000)) #增加一個10位的數再求餘  
  7.     echo $(($num%$max+$min))  
  8. }  
  9.   
  10. rnd=$(rand 400000 500000)  
  11. echo $rnd  
  12.   
  13. 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.使用date +%s%N

例:產生1~50的隨機數

 

[plain] view plaincopyprint?
  1. #!/bin/bash  
  2.   
  3. function rand(){  
  4.     min=$1  
  5.     max=$(($2-$min+1))  
  6.     num=$(date +%s%N)  
  7.     echo $(($num%$max+$min))  
  8. }  
  9.   
  10. rnd=$(rand 1 50)  
  11. echo $rnd  
  12.   
  13. exit 0  
#!/bin/bashfunction rand(){    min=$1    max=$(($2-$min+1))    num=$(date +%s%N)    echo $(($num%$max+$min))}rnd=$(rand 1 50)echo $rndexit 0
3.使用/dev/random 和 /dev/urandom

 

/dev/random 儲存著系統當前運行環境的即時資料,是阻塞的隨機數發生器,讀取有時需要等待。

/dev/urandom 非阻塞隨機數發生器,讀取操作不會產生阻塞。

例:使用/dev/urandom產生100~500的隨機數,使用urandom避免阻塞。

 

[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 10 | cksum | awk -F ‘ ‘ ‘{print $1}‘)    echo $(($num%$max+$min))}rnd=$(rand 100 500)echo $rndexit 0
4.使用linux uuid

 

uuid 全稱是通用唯一識別碼,格式包含32個16進位數字,以‘-‘串連號分為5段。形式為8-4-4-4-12 的32個字元。

 

[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
例:使用linux uuid 產生100~500隨機數

 

 

[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 $1}‘)    echo $(($num%$max+$min))}rnd=$(rand 100 500)echo $rndexit 0
5.產生隨機字串

 

例:產生10位隨機字串

 

[plain] view plaincopyprint?
  1. #使用date 產生隨機字串  
  2. date +%s%N | md5sum | head -c 10  
  3.   
  4. #使用 /dev/urandom 產生隨機字串  
  5. cat /dev/urandom | head -n 10 | md5sum | head -c 10  

shell 產生指定範圍隨機數與隨機字串 .

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.