调用这个Math.Random()函数能够返回带正号的double值,取值范围是[0.0,1.0)的左闭右开区间,并在该范围内(近似)均匀分布。

  1. protected int next(int bits):生成下一个伪随机数。
  2. boolean nextBoolean():返回下一个伪随机数,它是取自此随机数生成器序列的均匀分布的boolean值。
  3. void nextBytes(byte[] bytes):生成随机字节并将其置于用户提供的 byte 数组中。
  4. double nextDouble():返回下一个伪随机数,它是取自此随机数生成器序列的、在0.0和1.0之间均匀分布的 double值。
  5. float nextFloat():返回下一个伪随机数,它是取自此随机数生成器序列的、在0.0和1.0之间均匀分布float值。
  6. double nextGaussian():返回下一个伪随机数,它是取自此随机数生成器序列的、呈高斯(“正态”)分布的double值,其平均值是0.0标准差是1.0。
  7. int nextInt():返回下一个伪随机数,它是此随机数生成器的序列中均匀分布的 int 值。
  8. int nextInt(int n):返回一个伪随机数,它是取自此随机数生成器序列的、在(包括和指定值(不包括)之间均匀分布的int值。
  9. long nextLong():返回下一个伪随机数,它是取自此随机数生成器序列的均匀分布的 long 值。
  10. void setSeed(long seed):使用单个 long 种子设置此随机数生成器的种子。
  1. public Random()  该构造方法使用一个和当前系统时间对应的相对时间有关的数字作为种子数,然后使用这个种子数构造Random对象。
  2. public Random(long seed) 该构造方法可以通过制定一个种子数进行创建。

  1.  种子数只是随机算法的起源数字,和生成的随机数字的区间无关;相同种子数的Random对象,相同次数生成的随机数字是完全相同的;
  2. 生成[0,n)区间的数有公式Math.abs(nextInt()%n)和nextInt(n)
  3. 生成任意区间[a,b),公式nextInt(b-a)+a和Math.abs(nextInt()%(b-a)+a
  4. 生成任意区间[a,b],公式nextInt(b+1-a)+a和Math.abs(nextInt()%(b+1-a)+a)

示例代码:

  1. package com.random;
  2. import java.util.Random;
  3. import org.junit.After;
  4. import org.junit.Assert;
  5. import org.junit.BeforeClass;
  6. import org.junit.Ignore;
  7. import org.junit.Test;
  8. public class testRandom {
  9. private static Random r1;
  10. private static Random r2;
  11. /**
  12. * @Title: loadUp
  13. * @Description: 测试之前的初始化工作
  14. */
  15. @BeforeClass
  16. public static void loadUp() {
  17. r1 = new Random(10);
  18. r2 = new Random(10);
  19. }
  20. @After
  21. public void testAfter() {
  22. System.out.println("------------------------>");
  23. }
  24. /**
  25. * @Title: testMathRandom
  26. * @Description: 通过Math.random产生[0,5)之间的数
  27. * @throws
  28. */
  29. @Ignore
  30. @Test
  31. public void testMathRandom() {
  32. for (int i = 0; i < 20; i++) {
  33. System.out.println((int) (Math.random() * 5));
  34. }
  35. }
  36. /**
  37. * @Title: testTwoRandom
  38. * @Description: 两个random对象,具有相同的种子,会产生相同的随机数(伪随机)
  39. */
  40. @Test
  41. public void testTwoRandom() {
  42. for (int i = 0; i < 10; ++i) {
  43. Assert.assertEquals(r1.nextInt(), r2.nextInt());
  44. }
  45. }
  46. /**
  47. * @Title: testRandom1
  48. * @Description: 产生[1,2.5)之间的数的,有公式 nextDouble()*(b-a)+a
  49. * @param 设定文件
  50. * @return void 返回类型
  51. * @throws
  52. */
  53. @Test
  54. @Ignore
  55. public void testRandom1() {
  56. for (int i = 0; i < 10; ++i) {
  57. System.out.println(r1.nextDouble() * 1.5 + 1);
  58. }
  59. }
  60. /**
  61. * @Title: testRandom2
  62. * @Description: 产生[0,10)的两种方法,生成[0,n)区间的数有公式Math.abs(nextInt()%n)和nextInt(n)
  63. */
  64. @Test
  65. @Ignore
  66. public void testRandom2() {
  67. for (int i = 0; i < 10; ++i) {
  68. System.out.println("方法一: " + r1.nextInt(10));
  69. System.out.println("方法二: " + Math.abs(r2.nextInt() % 10));
  70. }
  71. }
  72. /**
  73. * @Title: testNextBoolean
  74. * @Description: 生成一个随机的boolean值,true和false值均等
  75. */
  76. @Test
  77. @Ignore
  78. public void testNextBoolean() {
  79. for (int i = 0; i < 10; ++i) {
  80. System.out.println(r1.nextBoolean());
  81. }
  82. }
  83. /**
  84. * @Title: testNextInt
  85. * @Description: 生成一个-2^31~2^31-1之间的随机数
  86. */
  87. @Test
  88. @Ignore
  89. public void testNextInt() {
  90. for (int i = 0; i < 10; ++i) {
  91. System.out.println(Math.abs(r1.nextInt()));// 0~2^31-1
  92. System.out.println(r1.nextInt());// -2^31~2^31-1
  93. System.out.println(r1.nextInt(10));// [0,10),参数10为随机生成数字的上限
  94. }
  95. }
  96. /**
  97. * @Title: testNextDouble
  98. * @Description: 随机生成[0,1.0)区间的小数
  99. */
  100. @Test
  101. @Ignore
  102. public void testNextDouble() {
  103. for (int i = 0; i < 10; ++i) {
  104. System.out.println(r1.nextDouble());
  105. }
  106. }
  107. /**
  108. * @Title: testRandom3
  109. * @Description: 生成任意区间[a,b),公式nextInt(b-a)+a和Math.abs(nextInt()%(b-a)+a),例如区间[-3,15)
  110. */
  111. @Test
  112. @Ignore
  113. public void testRandom3() {
  114. for (int i = 0; i < 100; ++i) {
  115. System.out.println(r1.nextInt(18) - 3);
  116. System.out.println(Math.abs(r1.nextInt()%18)-3);
  117. }
  118. }
  119. /**
  120. * @Title: testRandom4
  121. * @Description: 生成任意区间[a,b],公式nextInt(b+1-a)+a和Math.abs(nextInt()%(b+1-a)+a),例如区间[3,10]
  122. */
  123. @Test
  124. public void testRandom4(){
  125. for(int i=0;i<20;++i){
  126. System.out.println(r1.nextInt(8)+3);
  127. }
  128. }
  129. }

      在前面的方法介绍中,nextInt(int n)方法中生成的数字是均匀的,也就是说该区间内部的每个数字生成的几率是相同的。那么如果生成一个[0,100)区间的随机整数,则每个数字生成的几率应该是相同的,而且由于该区间中总计有100个整数,所以每个数字的几率都是1%。按照这个理论,可以实现程序中的几率问题。

示例代码:

  1. @Test
  2. public void testRandom5() {
  3. for (int i = 0; i < 100; ++i) {
  4. int a = r1.nextInt(100);
  5. if (a < 55) {
  6. System.out.println("1");// 55%的几率
  7. } else if (a < 95) {
  8. System.out.println("2");// 40%的几率
  9. } else {
  10. System.out.println("3");// 5%的几率
  11. }
  12. }
  13. }

版权声明:本文为dmir原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/dmir/p/4887879.html