mardi 25 août 2015

Random math generation algorithm not working

Note that the question title is:

Random math question generation algorithm not working

because I can't enter "question" in question title...


So I am writing an android app that helps little kids learn maths by giving math questions to them. They can change the number of digits and the type of operation as they like. I am now confused with this algorithm that I wrote to generate random questions. It is generating the same question every time if with the same question options. For question options

number of digits: 1
operation: +

It is always 10 + 10.

Question options

number of digits: 2
operation: +

It is always 11 + 11.

Question options

number of digits: 3
operation: +

It is always 8 + 8.

Question options

number of digits: 2
operation: +

It is always 11 + 11.

Although the other operation types (-, +/-, *) are a bit more random, one of its operands is always the same. 1 digit is 10, 2 digits is 11, 3 digits is 8.

Now here is my algorithm:

public void generateAQuestion () {
    switch (options.getOperationType ()) {
        case ADDITION:
            current = generateAddition ();
            break;
        case SUBTRACTION:
            current = generateSubtraction ();
            break;
        case ADD_AND_SUB:
            current = generateAddAndSub ();
            break;
        case MULTIPLICATION:
            current = generateMultiplication ();;
            break;
    }
}

private int generateNumberWithDigitCount () {
    int minValue = 10 ^ (options.getDigitCount () - 1);
    int maxValue = 10 ^ options.getDigitCount () - 1;
    Random r = new Random ();
    return r.nextInt (maxValue - minValue + 1) + minValue;
}

private Question generateAddition () {
    int operand1, operand2;
    operand1 = generateNumberWithDigitCount ();
    operand2 = generateNumberWithDigitCount ();
    return new Question (operand1, operand2);
}

private Question generateSubtraction () {
    int operand1 = generateNumberWithDigitCount ();
    Random r = new Random ();
    int operand2 = -(r.nextInt (operand1));
    return new Question (operand1, operand2);
}

private Question generateAddAndSub () {
    Question firstPart;
    if (Math.random () > 0.5) {
        firstPart = generateAddition ();
    } else {
        firstPart = generateSubtraction ();
    }
    int[] operands = new int[3];
    operands[0] = firstPart.getOperands ()[0];
    operands[1] = firstPart.getOperands ()[1];

    if (Math.random () > 0.5) {
        Random r = new Random ();
        operands[2] = -(r.nextInt (firstPart.getAnswer ()));
    } else {
        operands[2] = generateNumberWithDigitCount ();
    }

    return new Question (operands);
}

private MultiplicationQuestion generateMultiplication () {
    return new MultiplicationQuestion (generateNumberWithDigitCount (), generateNumberWithDigitCount ());
}

private int[] generateAnswers (int correctAnswer) {
    int[] answers = new int[4];
    Random r = new Random ();
    int correctAnswerIndex = r.nextInt (4);
    answers[correctAnswerIndex] = correctAnswer;
    for (int i = 0 ; i < answers.length ; i++) {
        if (i == correctAnswerIndex) {
            continue;
        }

        if (Math.random () > 0.5) {
            answers[i] = correctAnswer + (i + 1);
        } else {
            int candidate = correctAnswer - (i + 1);
            if (candidate < 0) {
                candidate = correctAnswer + i + 5;
            }
            answers[i] = candidate;
        }
    }
    return answers;
}

Notes: options is the question options, its methods are self explanatory, you can get it.Don't care about the Question and MultiplicationQuestion's constructors, they are irrelevant.

And I don't know what I did wrong, everything makes sense to me. What did I do wrong?




Aucun commentaire:

Enregistrer un commentaire