Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中获取不重复的随机数。我做的那个太慢了 import java.util.Random; 导入java.util.ArrayList; 公共类随机化示例{ private ArrayList OccurrendNum=新建ArrayList(); 私有随机r=新随机(); 私用int play=5,repCounter=0; 私有布尔停止检查=false; 公共空间(英文){ 发生数加(玩); } public int getrandom无重复(){ 而(!停止检查){ play=r.nextInt(9)+1; 对于(内部e:发生数){ 如果(播放==e){ repCounter++; } } 如果(repCounter==0){ stopCheck=true; 发生数加(玩); } } repCounter=0; 停止检查=错误; 返回(播放); } 公共静态void main(字符串[]args){ RandomExample t=新的RandomExample(); t、 Tem(); System.out.println(t.GetRandomWithoutRepeation()); System.out.println(t.GetRandomWithoutRepeation()); System.out.println(t.GetRandomWithoutRepeation()); System.out.println(t.GetRandomWithoutRepeation()); System.out.println(t.GetRandomWithoutRepeation()); System.out.println(t.GetRandomWithoutRepeation()); System.out.println(t.GetRandomWithoutRepeation()); System.out.println(t.GetRandomWithoutRepeation()); } }_Java_Class_Random - Fatal编程技术网

在Java中获取不重复的随机数。我做的那个太慢了 import java.util.Random; 导入java.util.ArrayList; 公共类随机化示例{ private ArrayList OccurrendNum=新建ArrayList(); 私有随机r=新随机(); 私用int play=5,repCounter=0; 私有布尔停止检查=false; 公共空间(英文){ 发生数加(玩); } public int getrandom无重复(){ 而(!停止检查){ play=r.nextInt(9)+1; 对于(内部e:发生数){ 如果(播放==e){ repCounter++; } } 如果(repCounter==0){ stopCheck=true; 发生数加(玩); } } repCounter=0; 停止检查=错误; 返回(播放); } 公共静态void main(字符串[]args){ RandomExample t=新的RandomExample(); t、 Tem(); System.out.println(t.GetRandomWithoutRepeation()); System.out.println(t.GetRandomWithoutRepeation()); System.out.println(t.GetRandomWithoutRepeation()); System.out.println(t.GetRandomWithoutRepeation()); System.out.println(t.GetRandomWithoutRepeation()); System.out.println(t.GetRandomWithoutRepeation()); System.out.println(t.GetRandomWithoutRepeation()); System.out.println(t.GetRandomWithoutRepeation()); } }

在Java中获取不重复的随机数。我做的那个太慢了 import java.util.Random; 导入java.util.ArrayList; 公共类随机化示例{ private ArrayList OccurrendNum=新建ArrayList(); 私有随机r=新随机(); 私用int play=5,repCounter=0; 私有布尔停止检查=false; 公共空间(英文){ 发生数加(玩); } public int getrandom无重复(){ 而(!停止检查){ play=r.nextInt(9)+1; 对于(内部e:发生数){ 如果(播放==e){ repCounter++; } } 如果(repCounter==0){ stopCheck=true; 发生数加(玩); } } repCounter=0; 停止检查=错误; 返回(播放); } 公共静态void main(字符串[]args){ RandomExample t=新的RandomExample(); t、 Tem(); System.out.println(t.GetRandomWithoutRepeation()); System.out.println(t.GetRandomWithoutRepeation()); System.out.println(t.GetRandomWithoutRepeation()); System.out.println(t.GetRandomWithoutRepeation()); System.out.println(t.GetRandomWithoutRepeation()); System.out.println(t.GetRandomWithoutRepeation()); System.out.println(t.GetRandomWithoutRepeation()); System.out.println(t.GetRandomWithoutRepeation()); } },java,class,random,Java,Class,Random,它在输出中打印一个随机数(尽管我要求它在1-9之间不重复地打印8个不同的数字),然后进入一个永无止境的循环,同时试图找到下一个要打印的数字。输出中从未包含全部8个数字。我的方法似乎太慢了。有没有更好的方法可以在不重复数字5(arrayList的第一个值)和后面的任何值的情况下获取随机数。公共类随机数示例{ import java.util.Random; import java.util.ArrayList; public class RandomExample { private A

它在输出中打印一个随机数(尽管我要求它在1-9之间不重复地打印8个不同的数字),然后进入一个永无止境的循环,同时试图找到下一个要打印的数字。输出中从未包含全部8个数字。我的方法似乎太慢了。有没有更好的方法可以在不重复数字5(arrayList的第一个值)和后面的任何值的情况下获取随机数。

公共类随机数示例{
import java.util.Random;
import java.util.ArrayList;

public class RandomExample {
    private ArrayList<Integer> occurredNum = new ArrayList<Integer>();
    private Random r = new Random();
    private int play = 5, repCounter = 0;
    private boolean stopCheck = false;

    public void Tem() {
        occurredNum.add(play);
    }

    public int getRandomWithoutRepetition() {
        while (!stopCheck) {
            play = r.nextInt(9) + 1;

            for (int e: occurredNum) {
                if (play == e) {
                    repCounter++;
                }
            }
            if (repCounter == 0) {
                stopCheck = true;
                occurredNum.add(play);
            }
        }

        repCounter = 0;
        stopCheck = false;

        return (play);
    }

    public static void main(String[] args) {
        RandomExample t = new RandomExample();
        t.Tem();
        System.out.println(t.getRandomWithoutRepetition());
        System.out.println(t.getRandomWithoutRepetition());
        System.out.println(t.getRandomWithoutRepetition());
        System.out.println(t.getRandomWithoutRepetition());
        System.out.println(t.getRandomWithoutRepetition());
        System.out.println(t.getRandomWithoutRepetition());
        System.out.println(t.getRandomWithoutRepetition());
        System.out.println(t.getRandomWithoutRepetition());
    }
}
private ArrayList OccurrendNum=新建ArrayList(); 私有随机r=新随机(); 私人智力游戏=5; 私人布尔停止检查; 公共空间(英文){ 发生数加(玩); } public int getrandom无重复(){ //重置布尔标志 停止检查=错误; 而(!停止检查){ //获取随机数 play=r.nextInt(9)+1; //询问ArrayList random num是否是已添加的项=>如果ArrayList不包含基元数据类型,则应重写数据类型的equals(Object obj)方法 如果(!OccurrendNum.包含(播放)){ //如果未包含num=>将num添加到ArrayList 发生数加(玩); //设置停止检查标志 stopCheck=true; } } //返回新数值 返回(播放); } 公共静态void main(字符串[]args){ RandomExample t=新的RandomExample(); t、 Tem(); System.out.println(t.GetRandomWithoutRepeation()); System.out.println(t.GetRandomWithoutRepeation()); System.out.println(t.GetRandomWithoutRepeation()); System.out.println(t.GetRandomWithoutRepeation()); System.out.println(t.GetRandomWithoutRepeation()); System.out.println(t.GetRandomWithoutRepeation()); System.out.println(t.GetRandomWithoutRepeation()); System.out.println(t.GetRandomWithoutRepeation()); } }
您不需要
repCounter
,您的代码可读性不强,也没有充分利用Java的面向对象编程特性

请记住,现在您正在比较整数,因此用于检查对象是否存在于ArrayList中的for迭代代码并不长。但将来可能需要比较复杂的对象,因此在这种情况下,更好的方法是使用
ArrayList.contains()
方法,并重写对象
equals(object obj)
方法,该方法在
ArrayList.contains()比较期间调用

它打印一个随机数(即使我已经要求它打印) 打印1-9之间的8个不同数字,不重复)

如果不想重复相同的数字,则
nextInt()
中使用的参数在性能方面存在问题:

public class RandomExample {
    private ArrayList<Integer> occurredNum = new ArrayList<Integer>();
    private Random r = new Random();
    private int play = 5;
    private boolean stopCheck;

    public void Tem() {
        occurredNum.add(play);
    }

    public int getRandomWithoutRepetition() {

        // reset boolean flag
        stopCheck = false;

        while (!stopCheck) {

            // get random num
            play = r.nextInt(9) + 1;

            // asking to the ArrayList if random num is an item already added => if ArrayList contains no primitive data type you should override data type's equals(Object obj) method
            if (! occurredNum.contains(play)) {

                // if num is not contained => add num to ArrayList
                occurredNum.add(play);

                // set stop checking flag
                stopCheck = true;
            }

        }

        // return new num
        return (play);
    }

    public static void main(String[] args) {
        RandomExample t = new RandomExample();
        t.Tem();
        System.out.println(t.getRandomWithoutRepetition());
        System.out.println(t.getRandomWithoutRepetition());
        System.out.println(t.getRandomWithoutRepetition());
        System.out.println(t.getRandomWithoutRepetition());
        System.out.println(t.getRandomWithoutRepetition());
        System.out.println(t.getRandomWithoutRepetition());
        System.out.println(t.getRandomWithoutRepetition());
        System.out.println(t.getRandomWithoutRepetition());
    }
}
您不应使用常量值作为nextInt()的参数,否则将循环很长时间以查找尚未使用的数字。
这里有10个数字,想象一下有100或1000个数字。该解决方案在某种程度上根本无法扩展

这个想法是:

  • 使用
    nextInt()
    方法中的列表大小仅查找尚未检索到的值。nextInt()将返回列表的索引
  • 使用索引从列表中获取值
  • 正在从列表中删除元素
所以对于

下面是一个简单的代码,让您了解解决方案:

 while (!stopCheck) {
        play = r.nextInt(9) + 1;
公共类示例{
...
私有列表值stoget=newarraylist(Arrays.asList(1,2,3,4,5,6,7,8,9));
public int getrandom无重复(){
int index=r.nextInt(valuesToGet.size());
整数值=valuesToGet.get(索引);
值获取。删除(索引);
返回值;
}
...
}

< /代码> 如果您想进一步提高性能,请考虑使用<代码> HasSET//C> > /P>
 public class RandomExample {
   ...
    private List<Integer> valuesToGet = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9));


    public int getRandomWithoutRepetition() {
        int index = r.nextInt(valuesToGet.size());
        Integer value = valuesToGet.get(index);
        valuesToGet.remove(index);
        return value;
    }
    ...
  }
公共类示例{
私有HashSet occurrendnum=新HashSet();
私有随机r=新随机();
私人智力游戏=5;
公共空间(英文){
发生数加(玩);
}
public int getrandom无重复(){
智力游戏;
while(true){
play=r.nextInt(9)+1;
//检查哈希集是否包含该元素
如果(!OccurrendNum.包含(播放)){
accurrendNum.add(play);//添加新值
return play;//返回它
}
}
}
公共静态真空干管(Stri
public class RandomExample {

    private HashSet<Integer> occurredNum = new HashSet<>();
    private Random r = new Random();
    private int play = 5;

    public void Tem() {
        occurredNum.add(play);
    }

    public int getRandomWithoutRepetition() {
        int play;
        while (true) {
            play = r.nextInt(9) + 1;

            // Check if the hash set contains the element
            if (!occurredNum.contains(play)) {
                occurredNum.add(play); // add the new value
                return play; // return it
            }
        }
    }

    public static void main(String[] args) {
        RandomExample t = new RandomExample();
        t.Tem();
        System.out.println(t.getRandomWithoutRepetition());
        System.out.println(t.getRandomWithoutRepetition());
        System.out.println(t.getRandomWithoutRepetition());
        System.out.println(t.getRandomWithoutRepetition());
        System.out.println(t.getRandomWithoutRepetition());
        System.out.println(t.getRandomWithoutRepetition());
        System.out.println(t.getRandomWithoutRepetition());
        System.out.println(t.getRandomWithoutRepetition());
    }

}