Coding Practice

Write a Java program to generate unique random numbers

Generate Unique Random Numbers
Sample Output
Enter a limit of random number: 10
1
5
2
4
9
3
10
6
8
7
Source Code
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class Main {

    public static boolean isDuplicate(ArrayList<Integer> numbers, int randomNumber){
        
        for (int i = 0; i < numbers.size(); i++) {
            
            if (numbers.get(i) == randomNumber) {
                
                return true;
            }
        }
        
        return false;
    }
    
    public static void main(String args[]) {

        Random random = new Random();

        int uniqueNumberCount = 0, randomNumber, limit;
        
        ArrayList<Integer> pastNumber = new ArrayList<>();
        
        Scanner in = new Scanner(System.in);
        
        System.out.print("Enter a limit of random number: ");
        limit = in.nextInt();
        
        for (int i = 0; uniqueNumberCount < limit; i++) {
            
            randomNumber = random.nextInt(limit) + 1;
            
            if (!isDuplicate(pastNumber, randomNumber)) {
                
                System.out.println(randomNumber);
                
                pastNumber.add(uniqueNumberCount++, randomNumber);
            }
        }

    }
}
Sample Output
Enter a limit of random number: 5
5
4
2
1
3

No comments:

Post a Comment

Change Theme
X