Home > Java > JavaBase > body text

How to use java random number random

醉折花枝作酒筹
Release: 2022-01-12 15:46:05
Original
64568 people have browsed it

In java, the usage of random is "static double random()". The "random()" method is used to return a random number. The range of the random number is "0.0=

How to use java random number random

The operating environment of this tutorial: windows7 system, java10 version, DELL G3 computer.

There are several ways to generate random numbers in Java

Java.util.UUID-Generate unique strings

You can generate unique characters through the UUID class String, and is globally unique, all strings generated by computers are different. It is used as follows:

String uuid  =  UUID.randomUUID().toString();
Copy after login

Math.random()

The Math.random() method can generate double type numbers in the interval [0,1), which can be 0, but less than 1. Other common usage methods are as follows

生成double随机数:Math.random()
生成[0,100)之间的整数:(int)(Math.random()*100)
Copy after login

Enter the source code of Math.random() and its specific use is to call the nextDouble() method of Random.

public static double random() {
        return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble();
}
    
private static final class RandomNumberGeneratorHolder {
   static final Random randomNumberGenerator = new Random();
}
Copy after login

Random class

The Random class generates random numbers. Its main methods are as follows

How to use java random number random

The following example uses Random to generate 5 Random numbers within [0,100)

Random random = new Random();

//生成5个 0到100之间的数字。
System.out.println("生成5个 0到100之间的数字。 nextInt(100)");
for (int n = 0; n < 5; n++) {
    System.out.print(random.nextInt(100)+", ");
}
Copy after login

Notes on the use of Random

There are two ways for Random to generate random numbers, one without seeds and the other with seeds

There is the following piece of code, which defines two random numbers, sets the same seed, and returns a random integer within 100. The order of the random numbers generated by the two Randoms is the same.

Random random = new Random(100);
Random random1 = new Random(100);

//随机生成5个int数字  next()
System.out.println("随机生成5个int数字  next()");
for (int n = 0; n < 5; n++) {
    System.out.println(random.nextInt(100)+" "+random1.nextInt(100));
}

输出的结果如下:
15,15
50,50
74,74
88,88
91,91
Copy after login

The random numbers of Random are actually pseudo-random numbers. As long as the seed is determined, the sequence of the generated random numbers is consistent. Therefore, it is avoided that the two Random instances in the example generate random numbers with the same sequence. There are generally two ways

  • Do not specify a seed when generating a Random instance.

  • Generate a singleton Random class, and then generate random numbers.

Recommended related video tutorials: Java video tutorial

The above is the detailed content of How to use java random number random. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!