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

时间:2013-09-04 19:26:23

标签: java for-loop probability abstract-data-type

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

实际问题: “创建应用程序TestLuck;让用户输入随机整数范围的上限(书中说10,000,但你也应该用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);

包含方法:

// Returns true if element is in this IntLog,
// otherwise returns false.
public boolean contains(int element) {                  
    int location = 0;
    int counter = 0;
    while (location <= lastIndex) {
        if (element == log[location]) {  // if they match
            counter++;
            location++;
            if(counter == 2)
                return true;
        } else
            location++;
    }
    return false;
}

2 个答案:

答案 0 :(得分:1)

您不需要 contains()方法,因为这只需要更多时间来计算比较简单的内容。

问题是在匹配第一个数字之前必须生成多少个数字,但如果包含第一个数字,则需要考虑。例如。 {1,2,3,4,1} count = 5,或{1,2,3,4,1} count = 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);

如果您需要使用 contains()方法让我知道评论,我会编辑答案。

我还建议不要使用任何存储数据结构,在这种情况下是ADT的IntArrayLog (同样,我不知道你是否需要在课程中使用ADT);这样你的程序运行得更快:

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);

答案 1 :(得分:0)

我在你的代码中发现了一些奇怪的东西。

首先,在向n提供值之前,您将arrLog插入n

其次,您正在测试i == upperLimit-1循环后for是否向计数器添加1。如果for循环在最后一步中断(在这种情况下,您已将2添加到计数器中),则只会遇到此情况。

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

for (location=1; location<=lastIndex; location++) {
    if (element = log[location]) return true;
}
return false;

但是,将narrLog.getElement(0)

进行比较应该会更容易

P.S。我假设其他一切都已正确初始化。