如何防止随机数生成器重复数字?

时间:2021-09-28 06:50:12

How can I prevent a random number generator from repeating numbers?

如何防止随机数生成器重复数字?

Here's the part of my code that generates the random numbers:

这是我生成随机数的代码部分:

String [] number = {"1","2","3","4","5","6"};
Random random = new Random();
int Select = random.nextInt(number.length);
System.out.println(number[Select]);

Sometimes it repeats numbers, like "4", and then "4" again. I'd like it to be always a different number.

有时它会重复数字,如“4”,然后再重复“4”。我希望它总是一个不同的数字。

6 个解决方案

#1


3  

I would do it this way

我会这样做

class Generator {
    List<String> number = Arrays.asList("1", "2", "3", "4", "5", "6");
    int n = 0;

    Generator() {
        Collections.shuffle(number);
    }

    String next() {
        return number.get(n++);
    }

#2


2  

For such a small set of numbers (one that can easily fit in memory), I would:

对于这么小的一组数字(一个可以轻松放入记忆中的数字),我会:

  1. copy the numbers into an ArrayList<String>,
  2. 将数字复制到ArrayList 中,

  3. use Collections.shuffle to randomize this list
  4. 使用Collections.shuffle随机化此列表

  5. iterate over the values of the list
  6. 迭代列表的值

#3


1  

Here is my solution.. I used ArrayList instead of regular array since it's string..

这是我的解决方案..我使用ArrayList而不是常规数组,因为它是字符串..

import java.util.*;

class RandomWithoutRepeat {

    public static void main(String[] args) {
         ArrayList<String> number = new ArrayList<String>(
         Arrays.asList("1","2","3","4","5","6"));
         Random random = new Random();
         int size = number.size();
         int Select = random.nextInt(number.size());
         for(int i =0; i< size; i++)
         {
            Select = random.nextInt(number.size());
            System.out.println(number.get(Select));
            number.remove(Select);   
         }

    }
}

#4


0  

A simple way to do it could be:

一个简单的方法可能是:

store what values have already been generated in an array x
while new value is in the var x
    generate a new value again
use new value in the way you want

#5


0  

public static void main(String[] args) {

public static void main(String [] args){

Integer[] arr = new Integer[1000];
for (int i = 0; i < arr.length; i++) {
    arr[i] = i;
}
Collections.shuffle(Arrays.asList(arr));
System.out.println(Arrays.toString(arr));

}

#6


-1  

Hmm, I am not 100 percent into your code, but it seems as if you are printing out the length of a number, and not the number... why? Are you trying to prevent getting two 4 digit numbers in a row? Ok, I get it... nvm.

嗯,我不是100%的代码,但似乎你打印出一个数字的长度,而不是数字...为什么?你想阻止连续获得两个4位数字吗?好的,我明白了... nvm。

edit: ok, so make it so that the array removes the 'select' variable every time the loop is run( make a loop if you don't have it). Then, the number can't possibly be picked.

编辑:好的,所以使它成为每次循环运行时数组删除'select'变量(如果你没有循环,则进行循环)。然后,不能选择该号码。

#1


3  

I would do it this way

我会这样做

class Generator {
    List<String> number = Arrays.asList("1", "2", "3", "4", "5", "6");
    int n = 0;

    Generator() {
        Collections.shuffle(number);
    }

    String next() {
        return number.get(n++);
    }

#2


2  

For such a small set of numbers (one that can easily fit in memory), I would:

对于这么小的一组数字(一个可以轻松放入记忆中的数字),我会:

  1. copy the numbers into an ArrayList<String>,
  2. 将数字复制到ArrayList 中,

  3. use Collections.shuffle to randomize this list
  4. 使用Collections.shuffle随机化此列表

  5. iterate over the values of the list
  6. 迭代列表的值

#3


1  

Here is my solution.. I used ArrayList instead of regular array since it's string..

这是我的解决方案..我使用ArrayList而不是常规数组,因为它是字符串..

import java.util.*;

class RandomWithoutRepeat {

    public static void main(String[] args) {
         ArrayList<String> number = new ArrayList<String>(
         Arrays.asList("1","2","3","4","5","6"));
         Random random = new Random();
         int size = number.size();
         int Select = random.nextInt(number.size());
         for(int i =0; i< size; i++)
         {
            Select = random.nextInt(number.size());
            System.out.println(number.get(Select));
            number.remove(Select);   
         }

    }
}

#4


0  

A simple way to do it could be:

一个简单的方法可能是:

store what values have already been generated in an array x
while new value is in the var x
    generate a new value again
use new value in the way you want

#5


0  

public static void main(String[] args) {

public static void main(String [] args){

Integer[] arr = new Integer[1000];
for (int i = 0; i < arr.length; i++) {
    arr[i] = i;
}
Collections.shuffle(Arrays.asList(arr));
System.out.println(Arrays.toString(arr));

}

#6


-1  

Hmm, I am not 100 percent into your code, but it seems as if you are printing out the length of a number, and not the number... why? Are you trying to prevent getting two 4 digit numbers in a row? Ok, I get it... nvm.

嗯,我不是100%的代码,但似乎你打印出一个数字的长度,而不是数字...为什么?你想阻止连续获得两个4位数字吗?好的,我明白了... nvm。

edit: ok, so make it so that the array removes the 'select' variable every time the loop is run( make a loop if you don't have it). Then, the number can't possibly be picked.

编辑:好的,所以使它成为每次循环运行时数组删除'select'变量(如果你没有循环,则进行循环)。然后,不能选择该号码。