How to generate random arithmetic expressions for a game

I would like to know if you can help me with this problem in my game. I am currently using a lot of code, if-else, etc. In my code and I don't like it at all.

I would like to generate 2 random arithmetic expressions that have one of the forms similar to the ones below:

1) number

for example: 19

2) operation number number

for example: 22 * 4

3) (transaction number) transaction number

for example: (10 * 4) / 5

4) ((operation number) operation number) operation number

for example: ((25 * 2) / 10) - 2

After I have 2 arithmetic expressions, the game is to match them and determine which is greater.

I would like to know how I can randomly select numbers and operations for each arithmetic expression in order to have an integer result (not a float), and also that both expressions have as close results as possible. Individual numbers must not be higher than 30.

I mean, I would not want the result to be 1000 and the other 14 because they would probably be too easy to determine which side is larger, so they should be like this:

expression 1: ((25 + 15) / 10) * 4 (which is 16)

expression 2: ((7 * 2) + 10) / 8 (which is 3)

The results (16 and 3) are integers and are reasonably close to each other.

Possible operations: +, -, * and /

It could be combined between two epxressions with different shapes, for example

((7 * 2) + 10) / 8 and (18/3) * 2

I really appreciate all the help you can give me. Thanks in advance!

Sincerely.

+3


source to share


1 answer


I think a sane way to approach this is to start with a value for the grand total and recursively build a random expression tree to achieve that total, you can choose how many operators you want in each equation and ensure that all values ​​are integers. Plus, you can choose how close the two equations are to you, even making them equal if you like. I'll use your expression 1 as an example .

((25 + 15) / 10) * 4 = 16

      

Let's start with a summary 16

and make the root of our tree:

   16

      

To expand a node (leaf), we select operator

and set it to the value of node and create two children containing operands

. In this case, we choose multiplication as our operator.

Multiplication is the only operator that will actually cause problems when trying to store all the integers of the operands. We can satisfy this constraint by building a divisor table for integers in our range [1..30]

(or maybe a little more, as we'll see below). In this case, our table would tell us what divisors 16

are {2,4,8}

. (If the divisor list for our current value is empty, we can choose a different operator or a different sheet altogether.)

Let's pick a random divisor, say 4

, and set it as a child of right

our node. The left child is obviously value/right

also an integer.

    *
   / \
  4   4

      



Now we need to select another sheet to expand. We can randomly select a leaf, randomly walk the tree until we reach a leaf, randomly walk up and straight from our current child node ( left

) until we reach a leaf, or whatever.

In this case, our selection algorithm decides to expand the left child and the division operator. In the case of division, we generate a random number for the child right

(in this case 10

) and set left

to value*right

. (The order is important here! Not so for multiplication.)

     *
    / \
   Γ·    4
  / \
 40 10

      

This illustrates why I said that the divisor table might need to go outside our stated range, as some of the intermediate values ​​might be slightly larger than 30. You can tweak your code to avoid this, or make sure the larger values ​​are further expanded to reach the final equation.

In the example, we do this by selecting the leftmost child to expand using the addition operator. In this case, we can just pick a random integer in the range [1..value-1]

for the child right

and value-right

for left

.

      *
     / \
    Γ·    4
   / \
  +  10
 / \
25 15

      

You can repeat as many operations as you like. To reconstruct the final equation, you just need to traverse the tree in order. To parenthesize as in your examples, you have to place parentheses around the entire equation, leaving the interior (operator) node except for the root.

+3


source







All Articles