I want to generate math questions procedurally for a small education game im making for the fun of it. Although I don’t have any idea on how to do this? Please give me info how to do so.

I don’t get what is so hard in that? If you want to do like basic sums / multiplications, then just generate a random sign using a table and choosing one from it and use math.random to choose the two random numbers.

But I dont want to have it be like “random number here” + “random number here” i also want the + to change into something else, thats what I dont know how to do. Because they’re not strings and I can’t generate them nor know how to.

I also need the answer to the equation as well its just I need the + to change for example.

You can have the different functions defined for the four operations or more if you need. And then assign them with keys in a table, then choose a random one from the table and pass the values and make the function do the math.

Example:

```
local function Add(a, b)
return a+b
end
--Functions for other sums here
local Operations = {"Add" = Add, "Subtract", "Multiply", "Divide"}
local randomOperation = Operations[math.random(#Operations)]
randomOperation(math.random(100), math.random(100)) --Will run the function that was assigned to the random key it choose, and pass the two random numbers for the function to do the math.
```

Not sure if this might be helpful to you but I tried making a function that generate random math problem with a settable range and stuff.

```
local AmountOfNumbers = 4 -- Amount of numbers for each problem
local RangeOfNumbers = {-10, 30} -- Random Range
local function GenerateMathProblem()
local Answer = 0
local PrintProblem
local TableOfNumbers = {}
for i = 1, AmountOfNumbers do
table.insert(TableOfNumbers, math.random(RangeOfNumbers[1], RangeOfNumbers[2]))
end
for _,Number in pairs (TableOfNumbers) do
if PrintProblem ~= nil then
local Sign = math.random(1, 2)
if Sign == 1 then
Answer += Number
PrintProblem =
Number > 0
and tostring(PrintProblem) .. " + " .. Number
or tostring(PrintProblem) .. " + " .. "(" .. Number .. ")"
elseif Sign == 2 then
Answer -= Number
PrintProblem =
Number > 0
and tostring(PrintProblem) .. " - " .. Number
or tostring(PrintProblem) .. " - " .. "(" .. Number .. ")"
elseif Sign == 3 then
Answer *= Number
PrintProblem =
Number > 0
and tostring(PrintProblem) .. " * " .. Number
or tostring(PrintProblem) .. " * " .. "(" .. Number .. ")"
else
Answer /= Number
PrintProblem =
Number > 0
and tostring(PrintProblem) .. " / " .. Number
or tostring(PrintProblem) .. " / " .. "(" .. Number .. ")"
end
else
Answer = Number
PrintProblem = tostring(Number)
end
end
print(PrintProblem)
print(Answer)
end
GenerateMathProblem()
```

Result:

I probably made a mistake around multiplication and division though.

maybe try using this as a ref? lol

That does not appear to respect order of operations (BEDMAS/PEMDAS/your regional variant). You could fix the underlying issue, but for simplicity’s sake it might be easier to just put parentheses around each operation. Something like `(((x ? y) ? z) ? a)`

or whatever.