Home  >  Article  >  Java  >  How to generate random numbers in Java

How to generate random numbers in Java

藏色散人
藏色散人Original
2019-01-17 10:13:4836443browse

How to generate random numbers in Java: first put the import statement before the start of the program class; then create a Random object; and finally select a random number from a certain range.

How to generate random numbers in Java

The operating environment of this article: Windows 7 system, Dell G3 computer, Java version 8.0.

Generating a series of random numbers is one of the common tasks that come up from time to time. In Java, this can be achieved simply using the java.util.Random class.

As with any API class, the first step is to put the import statement before the program class starts:

 import java.util.Random;

Next, create a Random object:

 Random rand = new Random();

Random Object provides you with a simple random number generator. The object's methods provide the ability to select random numbers. For example, the nextInt() and nextLong() methods will return numbers within the range of values ​​(negative and positive numbers) of the int and long data types respectively:

 Random rand = new Random();
for (int j=0;j < 5;j++)
{
  System.out.printf("%12d ",rand.nextInt());
  System.out.print(rand.nextLong());
  System.out.println();
}

The returned number will be a randomly selected int and long value:

-1531072189 -1273932119090680678
1849305478  6088686658983485101
1043154343  6461973185931677018
1457591513  3914920476055359941
-1128970433 -7917790146686928828

Pick random numbers from a range

Typically, the random numbers to be generated need to be from a range (for example, including between 1 and 40). For this purpose, nextInt() method can also accept int parameter. It represents the upper limit of a numeric range. However, the cap number is not included in one of the numbers that can be selected. This may sound confusing, but the nextInt() method works from scratch. For example:

 Random rand = new Random();
rand.nextInt(40);

will only select a random number from 0 to 39. To select from a range starting with 1, just add 1 to the result of the nextInt() method. For example, to select a number between 1 and 40, add to the result a:

 Random rand = new Random();
int pickedNumber = rand.nextInt(40) + 1;

If the range starts with a higher number, you will need:

Subtract the upper number starting number, then add one.

Add the starting number to the result of the nextInt() method.

For example, to choose a number from 5 to 35, the upper limit number would be 35-5 1 = 31, and 5 would need to be added to the result:

 Random rand = new Random();
int pickedNumber = rand.nextInt(31) + 5;

How random is the random class ?

I should point out that the Random class generates random numbers in a deterministic manner. The algorithm that generates randomness is based on a number called a seed. If the seed number is known, it is possible to calculate the number that will result from the algorithm. To prove this, I will use the number of the day Neil Armstrong first set foot on the moon as my seed number (July 20, 1969):

 import java.util.Random;
public class RandomTest {;
  public static void main(String[] args) {
    Random rand = new Random(20071969);
    for (int j = 0; j

No matter who runs this code, the "random" generated The sequence of numbers will be:

 3 0 3 0 7 9 8 2 2 5

By default, the seed number used is given by:

 Random rand = new Random();

is the current time in milliseconds since January 1, 1970. Typically this will produce numbers that are random enough for most purposes. However, please note that two random number generators created within the same millisecond will generate the same random number.

Also be careful when using the Random class for any application that must have a secure random number generator (for example, a gambling program). The seed number can be guessed based on how long the application has been running. Generally, for applications where random numbers are absolutely critical, it's best to find alternatives to Random objects. For most applications that just require some random element (for example, dice for a board game), it works fine.

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

Statement:
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
Previous article:Why choose JavaNext article:Why choose Java