日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Java隨機(jī)數(shù)總結(jié)(第二部分)

四、Java隨機(jī)數(shù)的測試

成都創(chuàng)新互聯(lián)公司IDC提供業(yè)務(wù):樂山服務(wù)器托管,成都服務(wù)器租用,樂山服務(wù)器托管,重慶服務(wù)器租用等四川省內(nèi)主機(jī)托管與主機(jī)租用業(yè)務(wù);數(shù)據(jù)中心含:雙線機(jī)房,BGP機(jī)房,電信機(jī)房,移動(dòng)機(jī)房,聯(lián)通機(jī)房。

通過一個(gè)例子說明上面的用法

 
 
 
  1.   import java.util.Random;  
  2.  
  3.   /**  
  4.  
  5.   * Java隨機(jī)數(shù)測試  
  6.  
  7.   * User: leizhimin  
  8.  
  9.   * Date: 2008-11-19 17:52:50  
  10.  
  11.   */ 
  12.  
  13.   public class TestRandomNum {  
  14.  
  15.   public static void main(String[] args) {  
  16.  
  17.   randomTest();  
  18.  
  19.   testNoSeed();  
  20.  
  21.   testSeed1();  
  22.  
  23.   testSeed2();  
  24.  
  25.   }  
  26.  
  27.   public static void randomTest() {  
  28.  
  29.   System.out.println("--------------test()--------------");  
  30.  
  31.   //返回以毫秒為單位的當(dāng)前時(shí)間。  
  32.  
  33.   long r1 = System.currentTimeMillis();  
  34.  
  35.   //返回帶正號的 double 值,大于或等于 0.0,小于 1.0。  
  36.  
  37.   double r2 = Math.random();  
  38.  
  39.   //通過Random類來獲取下一個(gè)隨機(jī)的整數(shù)  
  40.  
  41.   int r3 = new Random().nextInt();  
  42.  
  43.   System.out.println("r1 = " + r1);  
  44.  
  45.   System.out.println("r3 = " + r2);  
  46.  
  47.   System.out.println("r2 = " + r3);  
  48.  
  49.   }  
  50.  
  51.   public static void testNoSeed() {  
  52.  
  53.   System.out.println("--------------testNoSeed()--------------");  
  54.  
  55.   //創(chuàng)建不帶種子的測試Random對象  
  56.  
  57.   Random random = new Random();  
  58.  
  59.   for (int i = 0; i < 3; i++) {  
  60.  
  61.   System.out.println(random.nextInt());  
  62.  
  63.   }  
  64.  
  65.   }  
  66.  
  67.   public static void testSeed1() {  
  68.  
  69.   System.out.println("--------------testSeed1()--------------");  
  70.  
  71.   //創(chuàng)建帶種子的測試Random對象  
  72.  
  73.   Random random = new Random(555L);  
  74.  
  75.   for (int i = 0; i < 3; i++) {  
  76.  
  77.   System.out.println(random.nextInt());  
  78.  
  79.   }  
  80.  
  81.   }  
  82.  
  83.   public static void testSeed2() {  
  84.  
  85.   System.out.println("--------------testSeed2()--------------");  
  86.  
  87.   //創(chuàng)建帶種子的測試Random對象  
  88.  
  89.   Random random = new Random();  
  90.  
  91.   random.setSeed(555L);  
  92.  
  93.   for (int i = 0; i < 3; i++) {  
  94.  
  95.   System.out.println(random.nextInt());  
  96.  
  97.   }  
  98.  
  99.   }  
  100.  
  101.   }  
  102.  

運(yùn)行結(jié)果:

 
 
 
  1. --------------test()--------------  
  2.  
  3.   r1 = 1227108626582 
  4.  
  5.   r3 = 0.5324887850155043 
  6.  
  7.   r2 = -368083737 
  8.  
  9.   --------------testNoSeed()--------------  
  10.  
  11.   809503475 
  12.  
  13.   1585541532 
  14.  
  15.   -645134204 
  16.  
  17.   --------------testSeed1()--------------  
  18.  
  19.   -1367481220 
  20.  
  21.   292886146 
  22.  
  23.   -1462441651 
  24.  
  25.   --------------testSeed2()--------------  
  26.  
  27.   -1367481220 
  28.  
  29.   292886146 
  30.  
  31.   -1462441651 
  32.  
  33.   Process finished with exit code 0 
  34.  

通過testSeed1()與testSeed2()方法的結(jié)果可以看到,兩個(gè)打印結(jié)果相同,因?yàn)樗麄兎N子相同,再運(yùn)行一次,結(jié)果還是一樣的,這就是帶種子隨機(jī)數(shù)的特性。而不帶種子的,每次運(yùn)行結(jié)果都是隨機(jī)的。

五、Java隨機(jī)數(shù)的綜合應(yīng)用

下面通過最近寫的一個(gè)隨機(jī)數(shù)工具類來展示用法:

 
 
 
  1.   import java.util.Random;  
  2.  
  3.   /**  
  4.  
  5.   * 隨機(jī)數(shù)、隨即字符串工具  
  6.  
  7.   * User: leizhimin  
  8.  
  9.   * Date: 2008-11-19 9:43:09  
  10.  
  11.   */ 
  12.  
  13.   public class RandomUtils {  
  14.  
  15.   public static final String allChar =            "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";  
  16.  
  17.   public static final String letterChar =         "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";  
  18.  
  19.   public static final String numberChar = "0123456789";  
  20.  
  21.   /**  
  22.  
  23.   * 返回一個(gè)定長的隨機(jī)字符串(只包含大小寫字母、數(shù)字)  
  24.  
  25.   *  
  26.  
  27.   * @param length 隨機(jī)字符串長度  
  28.  
  29.   * @return 隨機(jī)字符串  
  30.  
  31.   */ 
  32.  
  33.   public static String generateString(int length) {  
  34.  
  35.   StringBuffer sb = new StringBuffer();  
  36.  
  37.   Random random = new Random();  
  38.  
  39.   for (int i = 0; i < length; i++) {  
  40.  
  41.   sb.append(allChar.charAt(random.nextInt(allChar.length())));  
  42.  
  43.   }  
  44.  
  45.   return sb.toString();  
  46.  
  47.   }  
  48.  
  49.   /**  
  50.  
  51.   * 返回一個(gè)定長的隨機(jī)純字母字符串(只包含大小寫字母)  
  52.  
  53.   *  
  54.  
  55.   * @param length 隨機(jī)字符串長度  
  56.  
  57.   * @return 隨機(jī)字符串  
  58.  
  59.   */ 
  60.  
  61.   public static String generateMixString(int length) {  
  62.  
  63.   StringBuffer sb = new StringBuffer();  
  64.  
  65.   Random random = new Random();  
  66.  
  67.   for (int i = 0; i < length; i++) {  
  68.  
  69.   sb.append(allChar.charAt(random.nextInt(letterChar.length())));  
  70.  
  71.   }  
  72.  
  73.   return sb.toString();  
  74.  
  75.   }  
  76.  
  77.   /**  
  78.  
  79.   * 返回一個(gè)定長的隨機(jī)純大寫字母字符串(只包含大小寫字母)  
  80.  
  81.   *  
  82.  
  83.   * @param length 隨機(jī)字符串長度  
  84.  
  85.   * @return 隨機(jī)字符串  
  86.  
  87.   */ 
  88.  
  89.   public static String generateLowerString(int length) {  
  90.  
  91.   return generateMixString(length).toLowerCase();  
  92.  
  93.   }  
  94.  
  95.   /**  
  96.  
  97.   * 返回一個(gè)定長的隨機(jī)純小寫字母字符串(只包含大小寫字母)  
  98.  
  99.   *  
  100.  
  101.   * @param length 隨機(jī)字符串長度  
  102.  
  103.   * @return 隨機(jī)字符串  
  104.  
  105.   */ 
  106.  
  107.   public static String generateUpperString(int length) {  
  108.  
  109.   return generateMixString(length).toUpperCase();  
  110.  
  111.   }  
  112.  
  113.   /**  
  114.  
  115.   * 生成一個(gè)定長的純0字符串  
  116.  
  117.   *  
  118.  
  119.   * @param length 字符串長度  
  120.  
  121.   * @return 純0字符串  
  122.  
  123.   */ 
  124.  
  125.   public static String generateZeroString(int length) {  
  126.  
  127.   StringBuffer sb = new StringBuffer();  
  128.  
  129.   for (int i = 0; i < length; i++) {  
  130.  
  131.   sb.append('0');  
  132.  
  133.   }  
  134.  
  135.   return sb.toString();  
  136.  
  137.   }  
  138.  
  139.   /**  
  140.  
  141.   * 根據(jù)數(shù)字生成一個(gè)定長的字符串,長度不夠前面補(bǔ)0  
  142.  
  143.   *  
  144.  
  145.   * @param num 數(shù)字  
  146.  
  147.   * @param fixdlenth 字符串長度  
  148.  
  149.   * @return 定長的字符串  
  150.  
  151.   */ 
  152.  
  153.   public static String toFixdLengthString(long num, int fixdlenth) {  
  154.  
  155.   StringBuffer sb = new StringBuffer();  
  156.  
  157.   String strNum = String.valueOf(num);  
  158.  
  159.   if (fixdlenth - strNum.length() >= 0) {  
  160.  
  161.   sb.append(generateZeroString(fixdlenth - strNum.length()));  
  162.  
  163.   } else {  
  164.  
  165.   throw new RuntimeException("將數(shù)字" + num + "轉(zhuǎn)化為長度為" + fixdlenth + "的字符串發(fā)生異    常!");  
  166.  
  167.   }  
  168.  
  169.   sb.append(strNum);  
  170.  
  171.   return sb.toString();  
  172.  
  173.   }  
  174.  
  175.   /**  
  176.  
  177.   * 根據(jù)數(shù)字生成一個(gè)定長的字符串,長度不夠前面補(bǔ)0  
  178.  
  179.   *  
  180.  
  181.   * @param num 數(shù)字  
  182.  
  183.   * @param fixdlenth 字符串長度  
  184.  
  185.   * @return 定長的字符串  
  186.  
  187.   */ 
  188.  
  189.   public static String toFixdLengthString(int num, int fixdlenth) {  
  190.  
  191.   StringBuffer sb = new StringBuffer();  
  192.  
  193.   String strNum = String.valueOf(num);  
  194.  
  195.   if (fixdlenth - strNum.length() >= 0) {  
  196.  
  197.   sb.append(generateZeroString(fixdlenth - strNum.length()));  
  198.  
  199.   } else {  
  200.  
  201.   throw new RuntimeException("將數(shù)字" + num + "轉(zhuǎn)化為長度為" + fixdlenth + "的字符串發(fā)生異    常!");  
  202.  
  203.   }  
  204.  
  205.   sb.append(strNum);  
  206.  
  207.   return sb.toString();  
  208.  
  209.   }  
  210.  
  211.   public static void main(String[] args) {  
  212.  
  213.   System.out.println(generateString(15));  
  214.  
  215.   System.out.println(generateMixString(15));  
  216.  
  217.   System.out.println(generateLowerString(15));  
  218.  
  219.   System.out.println(generateUpperString(15));  
  220.  
  221.   System.out.println(generateZeroString(15));  
  222.  
  223.   System.out.println(toFixdLengthString(12315));  
  224.  
  225.   System.out.println(toFixdLengthString(123L, 15));  
  226.  
  227.   }  
  228.  
  229.   }  

運(yùn)行結(jié)果:

 
 
 
  1.   vWMBPiNbzfGCpHG  
  2.  
  3.   23hyraHdJkKPwMv  
  4.  
  5.   tigowetbwkm1nde  
  6.  
  7.   BPZ1KNEJPHB115N  
  8.  
  9.   000000000000000 
  10.  
  11.   000000000000123 
  12.  
  13.   000000000000123 
  14.  
  15.   Process finished with exit code 0 
  16.  

六、總結(jié)

1、隨機(jī)數(shù)很常用,在Java有三種產(chǎn)生方式,以Random隨機(jī)數(shù)的使用最為復(fù)雜。

2、Random類對象有是否帶種子之分,帶種子的只要種子相同,多次運(yùn)行,生成隨機(jī)數(shù)的結(jié)果總是那樣。

3、帶種子隨機(jī)數(shù)的帶種子的對象創(chuàng)建方式有兩種,效果一樣。但是帶種子的隨機(jī)數(shù)用處似乎不大。

4、Random的功能涵蓋了Math.random()的功能。

5、可以通過隨機(jī)數(shù)去做實(shí)現(xiàn)隨機(jī)字符串等復(fù)雜的隨機(jī)數(shù)據(jù)。

6、不要研究不重復(fù)的隨機(jī)數(shù),意義不大。

【編輯推薦】

  1. Java隨機(jī)數(shù)總結(jié)(***部分)
  2. 走進(jìn)Java 7中的模塊系統(tǒng)
  3. JavaFX 1.2 已經(jīng)發(fā)布 主要新功能一覽
  4. 2009年十大Java技術(shù)解決方案
  5. 2008最值得學(xué)習(xí)的五種JAVA技術(shù)

新聞標(biāo)題:Java隨機(jī)數(shù)總結(jié)(第二部分)
URL鏈接:http://www.dlmjj.cn/article/coegsid.html