📜  在Java中生成随机数

📅  最后修改于: 2022-05-13 01:55:12.628000             🧑  作者: Mango

在Java中生成随机数

Java提供了三种使用内置方法和类生成随机数的方法,如下所示:

  • Java.util.Random
  • Math.random方法:可以生成双精度类型的随机数。
  • ThreadLocalRandom

1) Java.util.Random
  • 为了使用此类生成随机数,我们必须首先创建此类的实例,然后使用该实例调用诸如 nextInt()、nextDouble()、nextLong() 等方法。
  • 我们可以使用这个类生成整数、浮点、双精度、长整数、布尔类型的随机数。
  • 我们可以将参数传递给方法,以便在要生成的数字范围上设置上限。例如,nextInt(6) 将生成 0 到 5 范围内的数字,包括 0 到 5。
      // A Java program to demonstrate random number generation
      // using java.util.Random;
      import java.util.Random;
        
      public class generateRandom{
        
          public static void main(String args[])
          {
              // create instance of Random class
              Random rand = new Random();
        
              // Generate random integers in range 0 to 999
              int rand_int1 = rand.nextInt(1000);
              int rand_int2 = rand.nextInt(1000);
        
              // Print random integers
              System.out.println("Random Integers: "+rand_int1);
              System.out.println("Random Integers: "+rand_int2);
        
              // Generate Random doubles
              double rand_dub1 = rand.nextDouble();
              double rand_dub2 = rand.nextDouble();
        
              // Print random doubles
              System.out.println("Random Doubles: "+rand_dub1);
              System.out.println("Random Doubles: "+rand_dub2);
          }
      }
      

      输出:

    Random Integers: 547
    Random Integers: 126
    Random Doubles: 0.8369779739988428
    Random Doubles: 0.5497554388209912
    


    2) Math.random()

    Math 类包含用于执行各种数值运算的各种方法,例如计算幂、对数等。其中一种方法是 random(),该方法返回一个带正号的双精度值,大于或等于 0.0 且小于 1.0 .返回值是伪随机选择的。此方法只能生成 Doubles 类型的随机数。下面的程序解释了如何使用这个方法:
    // Java program to demonstrate working of 
    // Math.random() to generate random numbers
    import java.util.*;
      
    public class generateRandom
    {
        public static void main(String args[])
        {
            // Generating random doubles
            System.out.println("Random doubles: " + Math.random());
            System.out.println("Random doubles: " + Math.random());
        }
    }
    

    输出:

    Random doubles: 0.13077348615666562
    Random doubles: 0.09247016928442775
    

    3) Java.util.concurrent.ThreadLocalRandom 类

    此类在Java 1.7 中引入,用于生成整数、双精度、布尔值等类型的随机数。下面的程序解释了如何使用此类生成随机数:
    // Java program to demonstrate working of ThreadLocalRandom
    // to generate random numbers.
    import java.util.concurrent.ThreadLocalRandom;
      
    public class generateRandom
    {
        public static void main(String args[])
        {
            // Generate random integers in range 0 to 999
            int rand_int1 = ThreadLocalRandom.current().nextInt();
            int rand_int2 = ThreadLocalRandom.current().nextInt();
      
            // Print random integers
            System.out.println("Random Integers: " + rand_int1);
            System.out.println("Random Integers: " + rand_int2);
      
            // Generate Random doubles
            double rand_dub1 = ThreadLocalRandom.current().nextDouble();
            double rand_dub2 = ThreadLocalRandom.current().nextDouble();
      
            // Print random doubles
            System.out.println("Random Doubles: " + rand_dub1);
            System.out.println("Random Doubles: " + rand_dub2);
      
            // Generate random booleans
            boolean rand_bool1 = ThreadLocalRandom.current().nextBoolean();
            boolean rand_bool2 = ThreadLocalRandom.current().nextBoolean();
      
            // Print random Booleans
            System.out.println("Random Booleans: " + rand_bool1);
            System.out.println("Random Booleans: " + rand_bool2);
        }
    }
    

    输出:

    Random Integers: 536953314
    Random Integers: 25905330
    Random Doubles: 0.7504989954390163
    Random Doubles: 0.7658597196204409
    Random Booleans: false
    Random Booleans: true
    

    参考:
    https://docs.oracle.com