隨機數產生

代碼如下:


  1. package com.zuidaima.core.util;  

  2. import java.util.Random;  

  3. publicclass RandomUtil {  

  4. publicstaticfinal String ALLCHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";  

  5. publicstaticfinal String LETTERCHAR = "abcdefghijkllmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";  

  6. publicstaticfinal String NUMBERCHAR = "0123456789";  

  7. /**

  8.     * 返回一個定長的隨機字符串(只包含大小寫字母、數字)

  9.     *

  10.     * @param length

  11.     *            隨機字符串長度

  12.     * @return 隨機字符串

  13.     */

  14. publicstatic String generateString(int length) {  

  15.        StringBuffer sb = new StringBuffer();  

  16.        Random random = new Random();  

  17. for (int i = 0; i < length; i++) {  

  18.            sb.append(ALLCHAR.charAt(random.nextInt(ALLCHAR.length())));  

  19.        }  

  20. return sb.toString();  

  21.    }  

  22. /**

  23.     * 返回一個定長的隨機純字母字符串(只包含大小寫字母)

  24.     *

  25.     * @param length

  26.     *            隨機字符串長度

  27.     * @return 隨機字符串

  28.     */

  29. publicstatic String generateMixString(int length) {  

  30.        StringBuffer sb = new StringBuffer();  

  31.        Random random = new Random();  

  32. for (int i = 0; i < length; i++) {  

  33.            sb.append(ALLCHAR.charAt(random.nextInt(LETTERCHAR.length())));  

  34.        }  

  35. return sb.toString();  

  36.    }  

  37. /**

  38.     * 返回一個定長的隨機純大寫字母字符串(只包含大小寫字母)

  39.     *

  40.     * @param length

  41.     *            隨機字符串長度

  42.     * @return 隨機字符串

  43.     */

  44. publicstatic String generateLowerString(int length) {  

  45. return generateMixString(length).toLowerCase();  

  46.    }  

  47. /**

  48.     * 返回一個定長的隨機純小寫字母字符串(只包含大小寫字母)

  49.     *

  50.     * @param length

  51.     *            隨機字符串長度

  52.     * @return 隨機字符串

  53.     */

  54. publicstatic String generateUpperString(int length) {  

  55. return generateMixString(length).toUpperCase();  

  56.    }  

  57. /**

  58.     * 生成一個定長的純0字符串

  59.     *

  60.     * @param length

  61.     *            字符串長度

  62.     * @return 純0字符串

  63.     */

  64. publicstatic String generateZeroString(int length) {  

  65.        StringBuffer sb = new StringBuffer();  

  66. for (int i = 0; i < length; i++) {  

  67.            sb.append('0');  

  68.        }  

  69. return sb.toString();  

  70.    }  

  71. /**

  72.     * 根據數字生成一個定長的字符串,長度不夠前面補0

  73.     *

  74.     * @param num

  75.     *            數字

  76.     * @param fixdlenth

  77.     *            字符串長度

  78.     * @return 定長的字符串

  79.     */

  80. publicstatic String toFixdLengthString(long num, int fixdlenth) {  

  81.        StringBuffer sb = new StringBuffer();  

  82.        String strNum = String.valueOf(num);  

  83. if (fixdlenth - strNum.length() >= 0) {  

  84.            sb.append(generateZeroString(fixdlenth - strNum.length()));  

  85.        } else {  

  86. thrownew RuntimeException("將數字" + num + "轉化爲長度爲" + fixdlenth  

  87.                    + "的字符串發生異常!");  

  88.        }  

  89.        sb.append(strNum);  

  90. return sb.toString();  

  91.    }  

  92. /**

  93.     * 每次生成的len位數都不相同

  94.     *

  95.     * @param param

  96.     * @return 定長的數字

  97.     */

  98. publicstaticint getNotSimple(int[] param, int len) {  

  99.        Random rand = new Random();  

  100. for (int i = param.length; i > 1; i--) {  

  101. int index = rand.nextInt(i);  

  102. int tmp = param[index];  

  103.            param[index] = param[i - 1];  

  104.            param[i - 1] = tmp;  

  105.        }  

  106. int result = 0;  

  107. for (int i = 0; i < len; i++) {  

  108.            result = result * 10 + param[i];  

  109.        }  

  110. return result;  

  111.    }  

  112. publicstaticvoid main(String[] args) {  

  113.        System.out.println("返回一個定長的隨機字符串(只包含大小寫字母、數字):" + generateString(10));  

  114.        System.out  

  115.                .println("返回一個定長的隨機純字母字符串(只包含大小寫字母):" + generateMixString(10));  

  116.        System.out.println("返回一個定長的隨機純大寫字母字符串(只包含大小寫字母):"

  117.                + generateLowerString(10));  

  118.        System.out.println("返回一個定長的隨機純小寫字母字符串(只包含大小寫字母):"

  119.                + generateUpperString(10));  

  120.        System.out.println("生成一個定長的純0字符串:" + generateZeroString(10));  

  121.        System.out.println("根據數字生成一個定長的字符串,長度不夠前面補0:"

  122.                + toFixdLengthString(123, 10));  

  123. int[] in = { 1, 2, 3, 4, 5, 6, 7 };  

  124.        System.out.println("每次生成的len位數都不相同:" + getNotSimple(in, 3));  

  125.    }  

  126. }  



有圖有真相:

20131128150225788.jpg


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章