[Java] How to get random numbers excluding specific numbers

9/13 update

Improved the code I wrote earlier. As an improvement point --Using Set instead of List to reduce the number of retries --The number you want to exclude is Interger type instead of String type

main.java


import java.util.Random;
import java.util.HashSet;

public class Main {
 
    public static void main(String[] args) {
        Random rand = new Random();
        int i;
        //Store the number you want to exclude
        Set exSet = new HashSet();
        exList.add(2);
	    exList.add(5);
	    exList.add(8);
		
		do {
			i = Interger.valueOf(rand.nextInt(10)+1); // 1~Generate random numbers up to 10 and convert to strings
		} while (exSet.contains(i)); //Repeat if it matches any of the numbers in the exclusion list
		System.out.println(i);
    }
}

Here is the code before improvement

main.java


import java.util.ArrayList;
import java.util.Random;

public class Main {
 
    public static void main(String[] args) {
        Random rand = new Random();
        String i;
        //Store the number you want to exclude
        List<String> exList = new ArrayList<String>();
        exList.add("2");
		exList.add("5");
		exList.add("8");
		
		do {
			i = String.valueOf(rand.nextInt(10)+1); // 1~Generate random numbers up to 10 and convert to strings
		} while (exList.contains(i)); //Repeat if it matches any of the numbers in the exclusion list
		System.out.println(i);
    }
}

I also found a way to exclude random numbers excluding specific numbers found on QA sites overseas. I will post it for reference.

public int getRandomWithExclusion(Random rnd, int start, int end, int... exclude) {
    int random = start + rnd.nextInt(end - start + 1 - exclude.length);
    for (int ex : exclude) {
        if (random < ex) {
            break;
        }
        random++;
    }
    return random;
}

main.java


int[] ex = { 2, 5, 6 };
val = getRandomWithExclusion(rnd, 1, 10, ex)
// 5,6,When a random number of 7 is generated, the value of 7 is acquired, so the frequency of occurrence is high only for 7.

Recommended Posts

[Java] How to get random numbers excluding specific numbers
How to use Java HttpClient (Get)
How to find Java prime numbers
[Java] How to get the current directory
How to get the date in java
[Java] How to get and output standard input
How to get Class from Element in Java
How to get and study java SE8 Gold
[Java] How to get to the front of a specific string using the String class
[Java] How to get the redirected final URL
[Java] How to get the authority of the folder
[Java] How to get the URL of the transition source
[Java] How to get the maximum value of HashMap
[Java] How to get a request by HTTP communication
As of April 2018 How to get Java 8 on Mac
[Java] How to erase a specific character from a character string
[Java] How to use Map
How to lower java version
[Java] How to use Map
How to uninstall Java 8 (Mac)
How to use java Optional
How to minimize Java images
How to write java comments
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to display Wingdings
[Java] How to use string.format
How to use Java Map
How to set Java constants
How to use Java variables
How to convert Java radix
[Java] How to implement multithreading
[Java] How to use Optional ①
How to initialize Java array
How to get the class name / method name running in Java
An unsupported Java version How to get rid of errors
How to study Java Silver SE 8
How to get started with slim
Studying Java # 6 (How to write blocks)
[Java] How to update Java on Windows
How to make a Java container
How to disassemble Java class files
How to use Java HttpClient (Post)
[Java] How to use join method
How to get parameters in Spark
[Processing × Java] How to use variables
[Java] How to create a folder
How to decompile java class files
[Java] How to use LinkedHashMap class
[JavaFX] [Java8] How to use GridPane
How to write Java variable declaration
How to use class methods [Java]
[Java] How to use List [ArrayList]
How to use classes in Java?
How to name variables in Java
How to get along with Rails
How to pass Oracle Java Silver
How to turn Iterator Dojo (Java)
java Eclipse How to debug javaScript
[Processing × Java] How to use arrays