How to generate 6 different random numbers in java

2020-02-06 03:56发布

I want to generate 6 different random numbers by using Math.random and store them into an array. How can I make sure that they are different? I know I need to use for-loop to check the array but how...

This is the range. I only need numbers between 1 and 49. ( 1 + (int) (Math.random() * 49) )

10条回答
女痞
2楼-- · 2020-02-06 04:17

Just keep generating numbers and adding them to the array as long as they are unique; psuedocode:

num = genNextRand()

For (array length)
    If (num not in array)
        addToArray()

Repeat while length not equal 6
查看更多
倾城 Initia
3楼-- · 2020-02-06 04:18

Generate any 6 numbers (not necessarily different). Order them.

a1 <= a2 <= a3 <= a4 <= a5 <= a6

Now take these 6 numbers

a1 < a2 + 1 < a3 + 2 < a4 + 3 < a5 + 4 < a6 + 5

These 6 are different and random.

The idea of this construct comes from some combinatorial proofs.

Its advantage is that it's simple, fast, and deterministic.
I think the time complexity is O(count*log(count)).
I wonder if it can be improved.

import java.util.TreeMap;

public class Test005 {

    public static void main(String[] args) {
        int count = 6;
        int min = 1;
        int max = 49;

        // random number mapped to the count of its occurrences
        TreeMap<Integer, Integer> mp = new TreeMap<Integer, Integer>();
        for (int i=0; i<count; i++){
             int d = ( min + (int) (Math.random() * (max-count+1)) );
             if (!mp.containsKey(d)){
                 mp.put(d, 0);
             }
             mp.put(d, mp.get(d) + 1);
        }

        // now ensure the output numbers are different
        int j = 0;
        for (int num : mp.keySet()){
            int cnt = mp.get(num);
            for (int i=0; i<cnt; i++){
                System.out.println(num + j);
                j++;
            }
        }
    }

}
查看更多
太酷不给撩
4楼-- · 2020-02-06 04:26

I've just came up with a small idea for Java 8-.

Set<Integer> set = new LinkedHashSet<>();
while(set.size() != 6)
    set.add(rnd.nextInt(49) + 1);
查看更多
叼着烟拽天下
5楼-- · 2020-02-06 04:28

Instead of checking that the array has no duplicates, you can use a bit more smartness while generating the numbers, such that uniqueness is enforced at the outset.

  1. Create a boolean[] as long as your range (49 entries);
  2. generate a random number from the full range;
  3. put that number into your output array;
  4. "cross out" the corresponding index in the boolean[];
  5. now generate another random number, but curtail the range by one (now 48);
  6. instead of directly using that number as output, scan your boolean[], counting all the non-crossed entries. Stop when you reach the count equal to the random number generated in step 5. The number corresponding to that entry is your output number;
  7. go to step 4.
查看更多
劳资没心,怎么记你
6楼-- · 2020-02-06 04:30

That code generate numbers from 6 to 0 and save in ArrayList.

If generated number was duplicated the program generate numbers again.

If generated number is different that number is added.

Code:

private ArrayList<Integer> arraylist = new ArrayList<Integer>();

private Random rand = new Random();

public void insertNumber() {
    while (true) {
        int i = generateNumber();
        if(!isGenerateNumberExists(i)){
            addNumber(i);
            break;
        }
    }
}
//Generate numbers
private int generateNumber() {
    return rand.nextInt(6);
}
//Confirm if that number exists
private boolean isGenerateNumberExists(int y) {
    for (int num : arraylist) {
        if (num == y) {
            return true;
        }
    }
    return false;
}
//Add number to arrayList
private void addNumber(int x) {
    arraylist.add(x);
}
查看更多
Ridiculous、
7楼-- · 2020-02-06 04:32

In Java 8:

final int[] ints = new Random().ints(1, 50).distinct().limit(6).toArray();

In Java 7:

public static void main(final String[] args) throws Exception {
    final Random random = new Random();
    final Set<Integer> intSet = new HashSet<>();
    while (intSet.size() < 6) {
        intSet.add(random.nextInt(49) + 1);
    }
    final int[] ints = new int[intSet.size()];
    final Iterator<Integer> iter = intSet.iterator();
    for (int i = 0; iter.hasNext(); ++i) {
        ints[i] = iter.next();
    }
    System.out.println(Arrays.toString(ints));
}

Just a little messier. Not helped by the fact that it's pretty tedious to unbox the Set<Integer> into an int[].

It should be noted that this solution should be fine of the number of required values is significantly smaller than the range. As 1..49 is quite a lot larger than 6 you're fine. Otherwise performance rapidly degrades.

查看更多
登录 后发表回答