Home > Java > JavaBase > random.nextint() detailed explanation

random.nextint() detailed explanation

hzc
Release: 2020-06-13 14:15:40
Original
53920 people have browsed it

random.nextint() detailed explanation

Usage of random.nextInt()

1. nextInt() without parameters will generate all valid integers (including positive numbers) , negative number, 0)

2. NextInt(int x) with parameters will generate an arbitrary positive integer in the range of 0~x (excluding X)

For example: int x =new Random.nextInt(100);

Then x is an arbitrary integer from 0 to 99

3. Generate an integer within the specified range

  /*
     * 生成[min, max]之间的随机整数
     * @param min 最小整数
     * @param max 最大整数
     */
    private static int randomInt(int min, int max){
        return new Random().nextInt(max)%(max-min+1) + min;
    }
Copy after login

For example: call The method randomInt(10,20); will generate an arbitrary integer within the [10,20] set

Recommended tutorial: "java tutorial"

The above is the detailed content of random.nextint() detailed explanation. 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