Home > Java > javaTutorial > body text

Java generates random double precision floating point numbers using nextDouble() function of Random class

王林
Release: 2023-07-25 09:06:31
Original
2431 people have browsed it

Java uses the nextDouble() function of the Random class to generate random double-precision floating-point numbers

The Random class in Java is a pseudo-random number generator that can be used to generate different types of random numbers. Among them, the nextDouble() function is used to generate a random double-precision floating point number.

Before using the Random class, we need to import the java.util package first. Next we can create a Random object and use the nextDouble() function to generate a random double-precision floating point number. The following is a sample code:

import java.util.Random;

public class RandomDemo {
    public static void main(String[] args) {
        Random random = new Random();
        double randomDouble = random.nextDouble();
        System.out.println("随机双精度浮点数: " + randomDouble);
    }
}
Copy after login

In this example, we first imported the java.util package, and then created a Random object random. Then call the nextDouble() function of random and assign the generated double-precision floating point number to the randomDouble variable. Finally, we use the System.out.println() function to print out the random number.

Run the above code and you will get an output similar to the following:

随机双精度浮点数: 0.725983783
Copy after login

Every time you run the program, you will get a different random double precision floating point number.

It should be noted that the random number generated by the nextDouble() function is a double-precision floating point number greater than or equal to 0 and less than 1. If we want to generate a random double precision floating point number within a specified range, we can use some simple mathematical operations. For example, if we want to generate a random double precision floating point number between 10 and 20, we can use the following code:

import java.util.Random;

public class RandomDemo {
    public static void main(String[] args) {
        Random random = new Random();
        double randomDouble = 10 + (20 - 10) * random.nextDouble();
        System.out.println("随机双精度浮点数: " + randomDouble);
    }
}
Copy after login

Run the above code and you will get a random double precision floating point number between 10 and 20 Double precision floating point number.

To summarize, the nextDouble() function of Java's Random class can generate random double-precision floating-point numbers, and the random numbers can be adapted to the required range through simple mathematical operations. By using the Random class properly, we can easily generate the random numbers we need in the program.

The above is the detailed content of Java generates random double precision floating point numbers using nextDouble() function of Random class. 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