Home > Java > javaTutorial > How to generate non-repeating random numbers every time in java

How to generate non-repeating random numbers every time in java

王林
Release: 2023-05-01 15:31:06
forward
3801 people have browsed it

1. Method description

Math.random() returns a double value of [0.0,1.0). Since the double value has higher precision, it can be regarded as random. Number, use (int) for type conversion to obtain an integer random number.

2. Example

public static void main(String[] args)
{
int max=100,min=1;
int ran2 = (int) (Math.random()*(max-min)+min);
System.out.println(ran2);
}
Copy after login

Sometimes we need some randomly generated numbers for use, and this java method has a way to generate random numbers. What this article brings is the Math.random() method, which can randomly generate some random numbers of integers.

What are the basic data types of java

The basic data types of Java are divided into:

1. Integer type, used to represent the data type of integer.

2. Floating point type, a data type used to represent decimals.

3. Character type. The keyword of character type is "char".

4. Boolean type is the basic data type that represents logical values.

The above is the detailed content of How to generate non-repeating random numbers every time in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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