How can I prevent the overlapping random numbers

2020-02-07 03:18发布

How would i prevent duplicating numbers from random numbers. I need to generate 5 numbers between 1 and 9 that are each different. I would often get same numbers like 23334, how can i prevent that? Any help would be great!

    int num2 = (int) Math.round((Math.random()*9) +1);
    int num1 = (int) Math.round((Math.random()*9) +1);
    int num5 = (int) Math.round((Math.random()*9) +1);
    int num3 = (int) Math.round((Math.random()*9) +1);
    int num4 = (int) Math.round((Math.random()*9) +1);

标签: java random
8条回答
ら.Afraid
2楼-- · 2020-02-07 03:31

Floyd's subset selection algorithm is designed to do exactly what you want, and is extremely efficient even for large sets. Selecting m items from a set of n is O(m) average running time, independent of n. Here's a Java implementation.

/*
 * Floyd's algorithm to chose a random subset of m integers
 * from a set of n, zero-based.
 */
public static HashSet<Integer> generateMfromN(int m, int n) {
   HashSet<Integer> s = new HashSet<Integer>();
   for (int j = n-m; j < n; ++j) {
      if(! s.add((int)((j+1) * Math.random()))) {
         s.add(j);
      }
   }
   return s;
}
查看更多
Rolldiameter
3楼-- · 2020-02-07 03:39
Set<Integer> set=new HashSet<>();
while (set.size()<5) {
    set.add( Math.round((Math.random()*9) +1));
}

After the set is filled you have 5 unique random numbers.

UPDATE: just to illustrate Jared Burrows' comment

查看更多
\"骚年 ilove
4楼-- · 2020-02-07 03:40
  1. Create a List includes the numbers that you want (1 to 9).
  2. Generate random number from 0 to (size of the list minus 1).
  3. Remove one element by index from the above generated random number. And add the removed element to a array which to be returned as a results

    public static void main(String[] args) {
         int []answers= returnRandomNonRepeatingNumbers(5,0,9);
         for(int answer: answers) {
            System.out.println(answer);
         }
    }
    public static int[] returnRandomNonRepeatingNumbers(int sizeYouWant, int poolStart, int poolEnd) {
        List<Integer> pool=new ArrayList<Integer>();
        for(int i=poolStart;i<=poolEnd;i++) {
           pool.add(i);
        }
    
        int []answers=new int[sizeYouWant];
    
        for(int i=0;i<sizeYouWant;i++) {
            //random index to be pick and remove from pool
            int randomIndex = (int) Math.round((Math.random()*(pool.size()-1)));
            answers[i]=pool.remove(randomIndex);
        }
    
        return answers;
    }
    
查看更多
太酷不给撩
5楼-- · 2020-02-07 03:47

One option is to use shuffle algorithm (e.g. Fisher-Yates shuffle ) to generate random sequence from 1 to 9, then take first 5 numbers of the sequence

Further explanation on StackOverflow: https://stackoverflow.com/a/196065/950427

查看更多
对你真心纯属浪费
6楼-- · 2020-02-07 03:51

I suppose you would need to store the ones that have been generated into an array and compare the new random number to the list to ensure it is unique.

public static void main (String[] args) throws java.lang.Exception
{
    // your code goes here
    int[] numbers = new int[5];
    int tempNumber = 0;

    for(int numberCounter = 0; numberCounter < numbers.length;)
    {
        tempNumber = (int) Math.round((Math.random()*9) +1);

        if(!contains(numbers, tempNumber)){
            numbers[numberCounter++] = tempNumber;
        }
    }
}

public static boolean contains(final int[] numbersArray, final int tempNumber) {
    for (final int numberFromArray : numbersArray) {
        if (numberFromArray == tempNumber) {
            return true;
        }
    }
    return false;
} 

I notice you did not use an array in your example, so in case you do not know how to use them yet, you could also make 5 variables.

int randomNumber = 0;

int firstNumber = Math.round((Math.random()*9) +1);
int secondNumber = 0;

while(secondNumber == 0){
    randomNumber = Math.round((Math.random()*9) +1)l
    if(randomNumber != firstNumber){
        secondNumber = randomNumber;
    }
}

And you could continue making while statements like that. But if you are supposed to know about arrays, you should definitely be using one to store the numbers.

查看更多
Root(大扎)
7楼-- · 2020-02-07 03:55

One possible approach to this problem can be divide & conquer. Step of following describes the approach:

  1. Say m is the minimum & n is the maximum, within what i wanna get x number of randoms
  2. Choose a random p between m & n. Save it to an array of answer. decrease x by 1 as we get one answer to our problem.
  3. Now take a q a random number between m & p-1, another r a random number between p+1 & n. Fill up the answer array with q & r decrease x 1 for q and another 1 for the r.
  4. Now carry on this process recursively, until the lower bound (m) & higher bound (n) becomes equal or x becomes 0.

Benefit: benefit of this approach is that, in worst case, it's runtime will be O(x), where x is the number of random number required. The best case scenarion is also o(x), as i have to find at least n number of random. These two comprise average case to θ(x) complexity.

import java.util.Random;
class GenerateDistinctRandom{
static int alreadyPut = 0;
static Random rand = new Random();

    public static int[] generateDistinctRandom(int howMany, int rangeMin, int rangeMax)
    {
        int randomNumbers[] = new int[howMany]; 
        GenerateDistinctRandom.recursiveRandomGenerator(rangeMin, rangeMax, randomNumbers, howMany);
        return randomNumbers;
    }

    private static void recursiveRandomGenerator(int rangeMin, int rangeMax, int[] storage ,int storageSize)
    {
        if(rangeMax - rangeMin <= 0 || GenerateDistinctRandom.alreadyPut == storageSize)
        {
            return ;
        }

    int randomNumber = GenerateDistinctRandom.rand.nextInt(rangeMax-rangeMin) + rangeMin;
    storage[GenerateDistinctRandom.alreadyPut] = randomNumber;
    GenerateDistinctRandom.alreadyPut++;

    //calling the left side of the recursion
    recursiveRandomGenerator(rangeMin, randomNumber - 1, storage, storageSize);
    recursiveRandomGenerator(randomNumber + 1, rangeMax, storage, storageSize);     
    }

    public static void main(String []args){
        int howMany = 5;
        int distinctNumber[] = GenerateDistinctRandom.generateDistinctRandom(howMany 0, 9);

        for(int i = 0;i < howMany;i++)
        {
            System.out.println(distinctNumber[i]);
        }

    }
}
查看更多
登录 后发表回答