R
R
Risin02016-03-04 03:07:03
Java
Risin0, 2016-03-04 03:07:03

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
}

For example:
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

Priorities of multiplication / division do not need to be taken into account, it will be considered roughly - from left to right.
Now I use the clumsy version: [random part] [action] [calculated value] = [result], but this option is not suitable because it comes out very similar.
Maybe you have other ideas?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
nirvimel, 2016-03-04
@Risin0

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.

E
evgeniy_lm, 2016-03-04
@evgeniy_lm

It's very simple, after receiving each value, initialize the random number generator

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question