Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.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中的生日悖论无法将运行次数增加100_Java - Fatal编程技术网

Java中的生日悖论无法将运行次数增加100

Java中的生日悖论无法将运行次数增加100,java,Java,我正在用Java编写一个生日悖论程序。我在使用for循环以100为单位递增一个集合时遇到了问题,因此它最多只能递增到10000。每次运行for循环时,它都在一组中运行1000实验 /** * The class BirthdayParadox is used to * simulated the so-called Birthday paradox, and uses * the class Statistics to store the results of * the experiment

我正在用Java编写一个生日悖论程序。我在使用for循环以
100
为单位递增一个集合时遇到了问题,因此它最多只能递增到
10000
。每次运行for循环时,它都在一组中运行
1000
实验

/**
* The class  BirthdayParadox is used to
* simulated the so-called Birthday paradox, and uses
* the class Statistics to store the results of
* the experiments.
*/

public class BirthdayParadox {


/** 
    * Random generator 
    */
private static java.util.Random generator = new java.util.Random();

/**
* Instance of the class Statistics
*/
private static Statistics stats;

/** 
    * Runs the series of experiments, and stores the result into
    * a Statistics object
    * 
    * @param range the size of the set from which random number are drawn
    * @param numberOfRuns the number of experiments to run
 *
 * @return a reference to a Statistics instance that holds the result
 * of the experiment
    */
public static Statistics runExperiments(int range, int numberOfRuns) {
     for (int i=0;i<numberOfRuns;i++) {
            stats.updateStatistics(oneRun(range));
     }
     return stats;
}

/** 
    * Runs a single experiment.
    * The parameter range defines the size of the set from which
    * the experiment is drawn
    * 
    * @param range the size of the set from which random number are drawn
    *
 * @return the number of random draw in the set that the method 
 * used before drawing the same element for the second time
    */

private static int oneRun(int range){
     int counter=0;
     boolean foundTwice=false;
     int[] numberFound=new int[range];
     int number=0;
     while (foundTwice==false) {
            number=generator.nextInt(range);
            if (numberFound[number]==1) {
                foundTwice=true;
            }
            numberFound[number]++;
            counter++;
     }
     return counter;
}


/** 
 * Main method. Runs the experiments numberOfRunstimes,
 * with increasingly large sets (increment in size:step).
 * Stop once the size reaches max.
 * plots the result.
 * 
 * @param args if not empty, contains the runtime values for
 * step, max and numberOfRuns
 */

public static void main(String[] args) {
     if (args.length>1) {
            stats = new Statistics(Integer.parseInt(args[1]));
            stats = runExperiments(Integer.parseInt(args[0]), Integer.parseInt(args[1]));
            System.out.println(stats);
     }
     else {
        stats = new Statistics(10000);
        for (int i=100;i<10001;i+=100) {
            //stats = runExperiments(i,10000);
        }
            System.out.println(stats);
     }
}
}

stats=新统计数据(10000)-是否有为10000个元素创建数组的方法?如果是-我猜你索引他们从0到9999。
按照你的
循环,最后一个要处理的元素是10000,但你没有这样的元素

可能
stats=新统计(10001)将解决问题

UPD: 如果
range
大于或等于
numberOfRuns
(在
runExperiments
方法中),您也可以得到一个异常,您可能需要在此处添加验证/前提条件

oneRun(10000)可能返回值2..10001(包括)(!)

可能您想在这里打破循环:

if (numberFound[number]==1) {
  foundTwice=true;
  break;
}

这个错误发生在哪里?您甚至没有从所讨论的循环中调用
runExperiments
。向我们展示堆栈traceIn IntelliJ您甚至可以在
ArrayIndexOutOfBoundsException
上添加断点,或者在
n=10000
处添加条件断点,这样您就可以更轻松地检查错误。我不知道日食的事,对不起。无论如何,提供stacktrace,比如@j.seashell说它将非常有帮助
Statistics
不一定是数组。@scott确实不是,这只是我的幸运(我希望)猜测)对不起,我将给你另一个我已经实现的类,我已经尝试过10001.public class Statistics{public static int numberOfRuns;public static int[]统计信息;公共静态int i=0;公共静态int tmp;公共静态int最小值=99999999;公共静态int最大值=0;公共统计信息(int numberOfRuns){this.numberOfRuns=numberOfRuns;stats=new int[numberOfRuns];}公共无效更新统计信息(int value){stats[i]=value;}@加雷图里奇提出了这个问题。你不可能指望任何人理智地解释这个问题。
if (numberFound[number]==1) {
  foundTwice=true;
  break;
}