Answer the question
In order to leave comments, you need to log in
Generating random math expression based on result?
Hello.
Such a question came up: How can one generate random maths with customizable difficulty based on the result?
Roughly speaking, there is a number 125 , we feed it to an abstract function:
/*List<AbstractToken>*/ String generateExpression(int result,int step){
// code
}
generateExpression(125, 1) = 124 + 1
generateExpression(125, 1) = 25 * 5
generateExpression(125, 1) = 1250 / 10
generateExpression(125, 10) = 10 * 5 + 5 * 2 + 10 + 1 + 1 + 1 + 2 + 1 - 1
Answer the question
In order to leave comments, you need to log in
Your algorithm needs to be improved quite a bit: For each of the numbers in the resulting expression, recursively apply the same expression generation function, and, the resulting sequences of actions, glue as strings are glued (it is convenient to use linked lists for this).
The difficulty setting can be to set a fixed recursion depth or to set the distribution coefficient of a random variable, which determines whether at a particular step to go further into the depth of the recursion, or return the current result to the top.
And the arrangement of brackets is not difficult to fasten.
It's very simple, after receiving each value, initialize the random number generator
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question