自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Java隨機(jī)數(shù)總結(jié)(第二部分)

開發(fā) 后端
本文是Java隨機(jī)數(shù)總結(jié)的第二部分,這一部分講到了Java隨機(jī)數(shù)的測試、綜合運(yùn)用,以及對這兩部分所講內(nèi)容的總結(jié)。

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

通過一個例子說明上面的用法

  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)前時間。  
  32.  
  33.   long r1 = System.currentTimeMillis();  
  34.  
  35.   //返回帶正號的 double 值,大于或等于 0.0,小于 1.0。  
  36.  
  37.   double r2 = Math.random();  
  38.  
  39.   //通過Random類來獲取下一個隨機(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é)果可以看到,兩個打印結(jié)果相同,因為他們種子相同,再運(yùn)行一次,結(jié)果還是一樣的,這就是帶種子隨機(jī)數(shù)的特性。而不帶種子的,每次運(yùn)行結(jié)果都是隨機(jī)的。

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

下面通過最近寫的一個隨機(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.   * 返回一個定長的隨機(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.   * 返回一個定長的隨機(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.   * 返回一個定長的隨機(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.   * 返回一個定長的隨機(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.   * 生成一個定長的純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ù)字生成一個定長的字符串,長度不夠前面補(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ù)字生成一個定長的字符串,長度不夠前面補(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ù)

 

 

 

責(zé)任編輯:仲衡 來源: 百度博客
相關(guān)推薦

2019-04-11 10:50:26

前端JavaScript開發(fā)

2009-06-12 10:48:33

Java Date

2009-06-09 15:00:51

Javascript表單驗證

2013-09-17 09:45:55

編程

2013-12-13 13:16:42

LinuxLinux面試題

2009-06-12 10:18:59

StaticJava

2013-04-08 16:16:59

Backbone.jsCRUD

2025-04-24 01:10:00

RAGAI人工智能

2015-06-17 11:33:58

數(shù)據(jù)中心模塊化

2009-06-15 13:47:09

Java Applet插件

2009-06-11 15:25:39

Java隨機(jī)數(shù)

2018-12-20 08:20:43

物聯(lián)網(wǎng)供應(yīng)鏈IOT

2009-02-23 18:00:18

CCNA視頻教程

2009-08-21 09:03:18

網(wǎng)易魔獸玩家流失

2012-05-25 10:45:16

創(chuàng)業(yè)視頻

2009-07-30 14:32:18

ASP.NET常用代碼

2010-10-27 13:19:30

程序員軟考模擬題答案

2010-10-20 13:19:21

2010年下半年軟考網(wǎng)絡(luò)工程師

2015-10-13 10:00:58

Swift隨機(jī)數(shù)使用總結(jié)

2014-01-21 09:42:32

Python代碼對象
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號