While there are ways to generate random characters, I’m not sure there is a way to then use that character as a mathematical operator so I don’t think that is what you need here.
I would just use a random number as the condition in an if statement which decides which operation to perform:
local ran = math.random(1, 4) if ran == 1 then --do addition elseif ran == 2 then --do subtraction elseif ran == 3 then --do multiplication elseif ran == 4 then --do division end
Just in case anyone else looks here in future for an answer about random characters, you could either do something like this which picks a random character using string.char and is useful for making a random password or something like that:
function makeString(length) if length \< 1 then return "" end local s = "" for i = 1, length do -- Generate random number from 32 to 126, turn it into character and add to string local ran = math.random(32, 126) --don't use numbers which give characters ' " \ ` as they may cause problems while ran == 34 or ran == 39 or ran == 92 or ran == 96 do ran = math.random(32, 126) end s = s .. string.char(ran) end return s end local str = makeString(12) print(str)
Or if you need a random character from a fixed range of characters, just put them into a table and then pick a random index from the table:
local function getRandomChar() local validChars = {"+", "-", "\*", "/"} return validChars[math.random(1, #validChars)] end local myRandomChar = getRandomChar()