Generating Unique Random Numbers in Java

后端 未结 21 2569
不思量自难忘°
不思量自难忘° 2020-11-21 07:45

I\'m trying to get random numbers between 0 and 100. But I want them to be unique, not repeated in a sequence. For example if I got 5 numbers, they should be 82,12,53,64,32

相关标签:
21条回答
  • 2020-11-21 08:02

    You can use the Collections class.

    A utility class called Collections offers different actions that can be performed on a collection like an ArrayList (e.g., search the elements, find the maximum or minimum element, reverse the order of elements, and so on). One of the actions it can perform is to shuffle the elements. The shuffle will randomly move each element to a different position in the list. It does this by using a Random object. This means it's deterministic randomness, but it will do in most situations.

    To shuffle the ArrayList, add the Collections import to the top of the program and then use the Shuffle static method. It takes the ArrayList to be shuffled as a parameter:

    import java.util.Collections;
    import java.util.ArrayList;
    public class Lottery {
    public static void main(String[] args) {
    //define ArrayList to hold Integer objects
    ArrayList numbers = new ArrayList();
    for(int i = 0; i < 100; i++)
    {
    numbers.add(i+1);
    }
    Collections.shuffle(numbers);
    System.out.println(numbers);
    }
    }
    
    0 讨论(0)
  • 2020-11-21 08:06
    1. Create an array of 100 numbers, then randomize their order.
    2. Devise a pseudo-random number generator that has a range of 100.
    3. Create a boolean array of 100 elements, then set an element true when you pick that number. When you pick the next number check against the array and try again if the array element is set. (You can make an easy-to-clear boolean array with an array of long where you shift and mask to access individual bits.)
    0 讨论(0)
  • 2020-11-21 08:08

    I re-factored Anand's answer to make use not only of the unique properties of a Set but also use the boolean false returned by the set.add() when an add to the set fails.

    import java.util.HashSet;
    import java.util.Random;
    import java.util.Set;
    
    public class randomUniqueNumberGenerator {
    
        public static final int SET_SIZE_REQUIRED = 10;
        public static final int NUMBER_RANGE = 100;
    
        public static void main(String[] args) {
            Random random = new Random();
    
            Set set = new HashSet<Integer>(SET_SIZE_REQUIRED);
    
            while(set.size()< SET_SIZE_REQUIRED) {
                while (set.add(random.nextInt(NUMBER_RANGE)) != true)
                    ;
            }
            assert set.size() == SET_SIZE_REQUIRED;
            System.out.println(set);
        }
    }
    
    0 讨论(0)
  • 2020-11-21 08:08

    I have made this like that.

        Random random = new Random();
        ArrayList<Integer> arrayList = new ArrayList<Integer>();
    
        while (arrayList.size() < 6) { // how many numbers u need - it will 6
            int a = random.nextInt(49)+1; // this will give numbers between 1 and 50.
    
            if (!arrayList.contains(a)) {
                arrayList.add(a);
            }
        }
    
    0 讨论(0)
  • 2020-11-21 08:08

    Check this

    public class RandomNumbers {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int n = 5;
            int A[] = uniqueRandomArray(n);
            for(int i = 0; i<n; i++){
                System.out.println(A[i]);
            }
        }
        public static int[] uniqueRandomArray(int n){
            int [] A = new int[n];
            for(int i = 0; i< A.length; ){
                if(i == A.length){
                    break;
                }
                int b = (int)(Math.random() *n) + 1;
                if(f(A,b) == false){
                    A[i++] = b;
                } 
            }
            return A;
        }
        public static boolean f(int[] A, int n){
            for(int i=0; i<A.length; i++){
                if(A[i] == n){
                    return true;
                }
            }
            return false;
        }
    }
    
    0 讨论(0)
  • 2020-11-21 08:09

    This will work to generate unique random numbers................

    import java.util.HashSet;
    import java.util.Random;
    
    public class RandomExample {
    
        public static void main(String[] args) {
            Random rand = new Random();
            int e;
            int i;
            int g = 10;
            HashSet<Integer> randomNumbers = new HashSet<Integer>();
    
            for (i = 0; i < g; i++) {
                e = rand.nextInt(20);
                randomNumbers.add(e);
                if (randomNumbers.size() <= 10) {
                    if (randomNumbers.size() == 10) {
                        g = 10;
                    }
                    g++;
                    randomNumbers.add(e);
                }
            }
            System.out.println("Ten Unique random numbers from 1 to 20 are  : " + randomNumbers);
        }
    }
    
    0 讨论(0)
提交回复
热议问题