Java随机数总结(第二部分)

开发 后端
本文是Java随机数总结的第二部分,这一部分讲到了Java随机数的测试、综合运用,以及对这两部分所讲内容的总结。

四、Java随机数的测试

通过一个例子说明上面的用法

  1.   import java.util.Random;  
  2.  
  3.   /**  
  4.  
  5.   * Java随机数测试  
  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.   //返回以毫秒为单位的当前时间。  
  32.  
  33.   long r1 = System.currentTimeMillis();  
  34.  
  35.   //返回带正号的 double 值,大于或等于 0.0,小于 1.0。  
  36.  
  37.   double r2 = Math.random();  
  38.  
  39.   //通过Random类来获取下一个随机的整数  
  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.   //创建不带种子的测试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.   //创建带种子的测试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.   //创建带种子的测试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.  

运行结果:

  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()方法的结果可以看到,两个打印结果相同,因为他们种子相同,再运行一次,结果还是一样的,这就是带种子随机数的特性。而不带种子的,每次运行结果都是随机的。

五、Java随机数的综合应用

下面通过最近写的一个随机数工具类来展示用法:

  1.   import java.util.Random;  
  2.  
  3.   /**  
  4.  
  5.   * 随机数、随即字符串工具  
  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.   * 返回一个定长的随机字符串(只包含大小写字母、数字)  
  24.  
  25.   *  
  26.  
  27.   * @param length 随机字符串长度  
  28.  
  29.   * @return 随机字符串  
  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.   * 返回一个定长的随机纯字母字符串(只包含大小写字母)  
  52.  
  53.   *  
  54.  
  55.   * @param length 随机字符串长度  
  56.  
  57.   * @return 随机字符串  
  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.   * 返回一个定长的随机纯大写字母字符串(只包含大小写字母)  
  80.  
  81.   *  
  82.  
  83.   * @param length 随机字符串长度  
  84.  
  85.   * @return 随机字符串  
  86.  
  87.   */ 
  88.  
  89.   public static String generateLowerString(int length) {  
  90.  
  91.   return generateMixString(length).toLowerCase();  
  92.  
  93.   }  
  94.  
  95.   /**  
  96.  
  97.   * 返回一个定长的随机纯小写字母字符串(只包含大小写字母)  
  98.  
  99.   *  
  100.  
  101.   * @param length 随机字符串长度  
  102.  
  103.   * @return 随机字符串  
  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.   * 根据数字生成一个定长的字符串,长度不够前面补0  
  142.  
  143.   *  
  144.  
  145.   * @param num 数字  
  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("将数字" + num + "转化为长度为" + fixdlenth + "的字符串发生异    常!");  
  166.  
  167.   }  
  168.  
  169.   sb.append(strNum);  
  170.  
  171.   return sb.toString();  
  172.  
  173.   }  
  174.  
  175.   /**  
  176.  
  177.   * 根据数字生成一个定长的字符串,长度不够前面补0  
  178.  
  179.   *  
  180.  
  181.   * @param num 数字  
  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("将数字" + num + "转化为长度为" + fixdlenth + "的字符串发生异    常!");  
  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.   }  

运行结果:

  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.  

六、总结

1、随机数很常用,在Java有三种产生方式,以Random随机数的使用最为复杂。

2、Random类对象有是否带种子之分,带种子的只要种子相同,多次运行,生成随机数的结果总是那样。

3、带种子随机数的带种子的对象创建方式有两种,效果一样。但是带种子的随机数用处似乎不大。

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

5、可以通过随机数去做实现随机字符串等复杂的随机数据。

6、不要研究不重复的随机数,意义不大。

【编辑推荐】

  1. Java随机数总结(***部分)
  2. 走进Java 7中的模块系统
  3. JavaFX 1.2 已经发布 主要新功能一览
  4. 2009年十大Java技术解决方案
  5. 2008最值得学习的五种JAVA技术

 

 

 

责任编辑:仲衡 来源: 百度博客
相关推荐

2019-04-11 10:50:26

前端JavaScript开发

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

2015-06-17 11:33:58

数据中心模块化

2009-06-11 15:25:39

Java随机数

2009-06-15 13:47:09

Java Applet插件

2018-12-20 08:20:43

物联网供应链IOT

2009-02-23 18:00:18

CCNA视频教程

2009-08-21 09:03:18

网易魔兽玩家流失

2009-07-30 14:32:18

ASP.NET常用代码

2012-05-25 10:45:16

创业视频

2010-10-27 13:19:30

程序员软考模拟题答案

2010-10-20 13:19:21

2010年下半年软考网络工程师

2015-10-13 10:00:58

Swift随机数使用总结

2014-01-21 09:42:32

Python代码对象

2010-10-28 14:11:55

网络规划师软考模拟题答案
点赞
收藏

51CTO技术栈公众号