How do you generate integer type random numbers in a specific range?

Asked 1 years ago, Updated 1 years ago, 142 views

You want to use Java to generate a random number of a specific range of int types.

For example:

You want to generate random numbers ranging from 5 to 10 with a minimum value of 5 and a maximum value of 10.

In Java, the random() method of the math class generates a random number of double types 0.0 and 1.0. The nextInt(int) method in the Random class generates a value of int type at least 0 and less than n. However, I cannot find a method that produces an integer random number between two numbers.

So here's what I've tried. But the problems still remain.

Solution 1:

randomNum = minimum + (int)(Math.random() * maximum); 

Problem:

randomNum can be greater than maximum.

Solution 2:

Random rn = new Random();
int n = maximum - minimum + 1;
int i = rn.nextInt() % n;
randomNum =  minimum + i;

Problem:

randomNum' can be less than minimum'.

How can we solve this problem?

These are the contents I searched on the Internet.

However, the problem could not be solved. Help me.

java-8 java range integer random

2022-09-22 13:49

1 Answers

Typical methods prior to Java 1.7 are as follows::

import java.util.Random;

/**
 * * Returns a pseudo-random number between min and max, inclusive.
 * * The difference between min and max can be at most
 * * <code>Integer.MAX_VALUE - 1</code>.
 *
 * * @param min Minimum value
 * * @param max Maximum value.  Must be greater than min.
 * * @return Integer between min and max, inclusive.
 * * @see java.util.Random#nextInt(int)
 */
public static int randInt(int min, int max) {

    // // NOTE: This will (intentionally) not run as written so that folks
    // // copy-pasting have to think about how to initialize their
    // // Random instance.  Initialization of the Random instance is outside
    // // the main scope of the question, but some decent options are to have
    // // a field that is initialized once and then re-used as needed or to
    // // use ThreadLocalRandom (if using at least Java 1.7).
    Random rand;

    // // nextInt is normally exclusive of the top value,
    // // so add 1 to make it inclusive
    int randomNum = rand.nextInt((max - min) + 1) + min;

    return randomNum;
}

Check out Related JavaDoc. In practice, the java.util.Random class often tends to be preferred over the java.lang.math.random() method.

In particular, you don't need to reimplement the ability to generate integers when you have a simple API within the standard library that performs the task.

After Java 1.7, you can easily obtain the desired value without having to set the start value clearly using the following methods:

import java.util.concurrent.ThreadLocalRandom;

// // nextInt is normally exclusive of the top value,
// // so add 1 to make it inclusive
ThreadLocalRandom.current().nextInt(min, max + 1);


2022-09-22 13:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.