Java中的随机数

来源:互联网 发布:淘宝网外衣女人 编辑:程序博客网 时间:2024/06/10 02:15

原文地址:Generating random numbers in Java

Java提供了通过下列三种内置方法或者类产生随机数字的方式:

  • java.util.Random类;
  • Math.random方法:可以生成浮点类型的随机数;
  • ThreadLocalRandom类;

1)java.util.Random

  • 用这个类可以产生随机数字,我们首先得创建这个类的实例,然后用这个实例调用nextInt(), nextDouble(), nextLong()等方法。
  • 我们可以用这个类产生integers, float, double, long, boolean类型的随机数。
  • 我们可以给这些方法串参数,来设置生成的随机数范围的上界。例如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: 547Random Integers: 126Random Doubles: 0.8369779739988428Random Doubles: 0.5497554388209912

2) Math.random()

Math类包含各种方法,这些方法可以去对各种数字进行操作,例如指数对数计算等。其中的一个方法就是random(),这个方法返回一个在0.0到1.0之间符号为正号的浮点数。返回值可以选为伪随机。这个方法只能产生Double类型的随机数,下面的代码解释了如何用这个方法:

// Java program to demonstrate working of // Math.random() to generate random numbersimport 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.13077348615666562Random doubles: 0.09247016928442775

3) java.util.concurrent.ThreadLocalRandom类

这个类是在Java 1.7引入的,它可以生成integers, doubles, booleans类型的随机数。下面的代码解释了如何用这个类生成随机数:

// 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: 536953314Random Integers: 25905330Random Doubles: 0.7504989954390163Random Doubles: 0.7658597196204409Random Booleans: falseRandom Booleans: true

参考文献

https://docs.oracle.com

0 0