A detailed introduction to the Java Random class (Java generates random numbers)


One, the use of Random class

Random class function, generate a random number

1. The basic syntax of Random class to generate random numbers (int):

(1) int取值范围Any number generated

Random r = new Random();
int num = r.nextInt();

(2) 指定范围Any number generated

Random r = new Random();
int num = r.nextInt(n);
//参数 n 代表指定范围的数字[0,n)

2. Several commonly used random number generation

method parameter Random number value range Return value type Instance
nextBoolean() no true、false boolean 1
nextDouble() no [0.0,1.0) double 2
nextFloat() no [0.0,1.0) float 3
nextInt () no The value range of int int 4
nextInt(int bound) Have The parameter represents the number of the specified range [0, bound) int 5
nextLong() no long value range long 6
nextBytes(byte[] bytes) Have Generate byte.length numbers into byte[] void 7
Code example-1
//生成Boolean随机数
Random r = new Random();
boolean b = r.nextBoolean();
System.out.println("生成的Boolean随机数是:"+b);
Code example-2
//生成double随机数
Random r = new Random();
double d = r.nextDouble();
System.out.println("生成的Double随机数是:"+d);
Code example-3
//生成Float随机数
Random r = new Random();
double f = r.nextFloat();
System.out.println("生成的Float随机数是:"+f);
Code example-4
//生成int随机数
Random r = new Random();
int i = r.nextInt();
System.out.println("生成的Int随机数是:"+i);
Code example-5
//生成[0,5)int随机数
Random r = new Random();
int i = r.nextInt(5);
System.out.println("生成[0,5)的]Int随机数是:"+i);
Code example-6
//生成Long随机数
Random r = new Random();
long l = r.nextLong();
System.out.println("生成的Long随机数是:"+l);
Code example-7
//将随机数字放入数组中
Random r = new Random();
byte[] arr = new byte[5];
r.nextBytes(arr);
for (int i = 0; i < arr.length; i++) {
    
    
    System.out.print(arr[i]+",");
}

Second, a detailed introduction to the Random class

1. Generate a specified range of numbers

左闭右闭 [a,b]r.nextInt(b-a+1)+a

Random r = new Random();
int a = 5;
int b = 10;
int res = r.nextInt(b-a+1)+a;
System.out.println(res);

左闭右开 [a,b)r.nextInt(b-a)+a

Random r = new Random();
int a = 5;
int b = 10;
int res = r.nextInt(b-a)+a;
System.out.println(res);

左开右闭 (a,b]r.nextInt(b-a)+a+1

Random r = new Random();
int a = 5;
int b = 10;
int res = r.nextInt(b-a)+a+1;
System.out.println(res);

左开右开 (a,b)r.nextInt(b-a-1)+a+1

Random r = new Random();
int a = 5;
int b = 10;
int res = r.nextInt(b-a-1)+a+1;
System.out.println(res);

2. The difference between Random.nextInt() and Math.random()

(1) Source 1

  • random.nextInt() is a method in the java.util.Random class;
  • Math.random() is a static method in the java.lang.Math class.

(2) Usage 1
Generate random numbers of [0,n)

//Random.nextInt()
Random r = new Random();
int r = random.nextInt(n);
//Math.random()
int res = (int)(Math.random() * n);

(3) Summary 1

  • The Math.random() method generates a double type random number in the range [0, 1); the nextXxxx series methods in the Random class generate a random number from 0 to n;
  • Math.random() is thread-safe and can be called in a multi-threaded environment;
  • If there is no special requirement, use (int)(Math.random()*n) to generate random numbers.

  1. csdn-basic usage of random.nextInt and Math.random ↩︎ ↩︎ ↩︎

Guess you like

Origin blog.csdn.net/weixin_44580492/article/details/105904140