Frage

I'm a student, trying to write a program that tests probability. It's called TestLuck it's supposed to generate a user determined amount of IntArrayLogs(ADT's) that are populated with random values. The program is supposed to calculate how many values were generated before there is a match to the first value.

Actual Problem: "Create application TestLuck; have the user enter the upper limit of the random integer range (the book says 10,000, but you should also test with 365) as well as the number of times to run the test. Compute and output the average."

This is what I came up with, but I'm not getting the correct results for some reason, I tested the methods I use and they seem to work right, I think it's something to do with how I'm keeping track of the counter.

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

Contains Method:

// 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;
}
War es hilfreich?

Lösung

You don't need a contains() method, as this will only take more time to compute something as simple as a comparison.

The question is how many numbers have to be generated before matching the first number, but you need to take into account if this includes the first number. eg. {1,2,3,4,1} count = 5, or {1,2,3,4,1} count = 4. Either way, this wont affect the logic on this answer:

If you re-arrange your method it will work much faster.

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

If you are required to use a contains() method let me know on the comments and I'll edit the answer.

I would also like to suggest not using any storage data structure, in this case an ADT's IntArrayLog (again, I dont know if you are required to use ADT as part of your course); so that your program will run even faster:

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

Andere Tipps

I find some odd things in your code.

First, you are inserting n to arrLog before giving n a value.

Second, you are testing if i == upperLimit-1 after the for loop to add 1 to the counter. You will only meet this condition if the for loop breaks in the last step (in which case you had added 2 to the counter).

Third, in the contains method you are returning true if you find element twice. As I understand the first time should be in position 0 (the first element) and then the test itself, yet you are passing the first element as argument. You should probably begin location in 1 (skipping first element) and count it once:

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

However it should be easier just to compare n with arrLog.getElement(0)

P.S. I'm assuming everything else is properly initialized.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top