How to generate random numbers in a specified range with Java

To generate a random integer between [min, Max],

random. Nextint (max) means generating random numbers between [0, Max], and then taking the modulus of (max min + 1).

Taking the generation of [10,20] random numbers as an example, first generate a random number of 0-20, then take the modulus of (20-10 + 1) to obtain a random number between [0-10], then add min = 10, and finally generate a random number of 10-20

Generate random numbers between 0 and 2, including 2

Generate random numbers between 5 and 26, including 26

Many places in the work will encounter the need to obtain random numbers within a specified range. Directly using the functions in the API given by java can not meet the requirements, and some changes need to be made.

Example: generate 10 random numbers within the specified range.

To generate a random integer between [min, Max]

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>