Java 在TestLuck(概率)程序中计算平均值

Java 在TestLuck(概率)程序中计算平均值,java,for-loop,probability,abstract-data-type,Java,For Loop,Probability,Abstract Data Type,我是一名学生,试图编写一个测试概率的程序。它被称为TestLuck,它应该生成由用户确定数量的intarraylog(ADT),并用随机值填充。程序应该计算在与第一个值匹配之前生成了多少个值 实际问题: “创建应用程序TestLuck;让用户输入随机整数范围的上限(书上说是10000,但您也应该使用365进行测试)以及运行测试的次数。计算并输出平均值。” 这就是我想出来的,但由于某些原因,我没有得到正确的结果,我测试了我使用的方法,它们似乎工作正常,我认为这与我如何跟踪计数器有关 for(int

我是一名学生,试图编写一个测试概率的程序。它被称为TestLuck,它应该生成由用户确定数量的intarraylog(ADT),并用随机值填充。程序应该计算在与第一个值匹配之前生成了多少个值

实际问题: “创建应用程序TestLuck;让用户输入随机整数范围的上限(书上说是10000,但您也应该使用365进行测试)以及运行测试的次数。计算并输出平均值。”

这就是我想出来的,但由于某些原因,我没有得到正确的结果,我测试了我使用的方法,它们似乎工作正常,我认为这与我如何跟踪计数器有关

for(int k=0; k<numTests; k++) {   
    for(int i=0; i<upperLimit; i++) {
        arrLog.insert(n);
        n = rand.nextInt(upperLimit);
        if(arrLog.contains(arrLog.getElement(0))) {
            totalCount += i;
            break;
        }
        if(i == upperLimit-1)
            totalCount +=i;
    }

    System.out.println("Total Count: " + totalCount);
    arrLog.clear();
}   
testAverage = totalCount/numTests;
System.out.println("Average tests before match: " + testAverage);

for(int k=0;k我在你的代码中发现了一些奇怪的东西

首先,在给
n
一个值之前,将
n
插入
arrLog

其次,您正在测试
i==upperLimit-1
是否在
for
循环后向计数器添加1。只有在上一步中
for
循环中断时(在这种情况下,您向计数器添加了2),您才会满足此条件

第三,在
contains
方法中,如果两次找到
元素,您将返回true。据我所知,第一次应该在位置0(第一个元素),然后是测试本身,但您将第一个元素作为参数传递。您可能应该在1(跳过第一个元素)中开始
位置
数一次:

for (location=1; location<=lastIndex; location++) {
    if (element = log[location]) return true;
}
return false;
for(location=1;location您不需要contains()方法,因为这只会花费更多的时间来计算像比较这样简单的东西

问题是在匹配第一个数字之前必须生成多少个数字,但您需要考虑这是否包括第一个数字。例如,{1,2,3,4,1}计数=5,或{1,2,3,4,1}计数=4。无论哪种方式,这都不会影响此答案的逻辑:

如果你重新安排你的方法,它会工作得更快

for(int k=0; k<numTests; k++){   
    for(int i=0; i<upperLimit; i++){
        arrLog.insert(n);
        if(arrLog.getElement(0) == n && i != 0){// i != 0 to prevent it from counting a match on the first iteration
            totalCount += i;//totalCount += i+1 if you are counting the first number
            break;
        }
        n = rand.nextInt(upperLimit);
    }
    System.out.println("Total Count: " + totalCount);
    arrLog.clear();
}   
testAverage = totalCount/numTests;
System.out.println("Average tests before match: " + testAverage);

for(int k=0;kHi)。要求人们发现代码中的错误并不是特别有效。您应该使用调试器(或添加打印语句)来隔离问题,方法是跟踪程序的进度,并将其与预期发生的情况进行比较。一旦这两种情况出现分歧,您就发现了问题。(如果必要的话,你应该构造一个)。我假设你在循环开始之前已经给出了一个值。另外,考虑用更高的数字替换你的for语句中的上限,以防产生的随机数超过上限,这将是非常罕见的情况,但是它可能会发生。
for(int k=0; k<numTests; k++){   
    for(int i=0; i<upperLimit; i++){
        arrLog.insert(n);
        if(arrLog.getElement(0) == n && i != 0){// i != 0 to prevent it from counting a match on the first iteration
            totalCount += i;//totalCount += i+1 if you are counting the first number
            break;
        }
        n = rand.nextInt(upperLimit);
    }
    System.out.println("Total Count: " + totalCount);
    arrLog.clear();
}   
testAverage = totalCount/numTests;
System.out.println("Average tests before match: " + testAverage);
int firstNum;
for(int k=0; k<numTests; k++){
    firstNum = rand.nextInt(upperLimit);
    for(int i=1; i<upperLimit; i++){//notice this starts in 1
        n = rand.nextInt(upperLimit);
        if(firstNum == n){
            totalCount += i;//totalCount += i+1 if you are counting the first number
            break;
        }
    }
    System.out.println("Total Count: " + totalCount);
    arrLog.clear();
}   
testAverage = totalCount/numTests;
System.out.println("Average tests before match: " + testAverage);