Random char generator similar to math.random

Hi,

Is there any way to generate random characters? (the way we use math.random(1,8) to generate a random number between 1 and 8.)

I am writing a simple equation, I just want to make the the equation random. I use math.random for the numbers, I also want the math operators (+, -, * and /) to be generated at random. Is that possible?

Thanks.

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()

Thanks, that explains a lot. 

Another similar query, math.random gives us one number from a range of numbers. What should I do if I want it to return one number out of, say, 3,5 and 7? 

Also, if I want to get any number in the range 1-20 EXCEPT 15, how can I do that?

thanks.

If you want to pick a number from a limited selection, just use the exact same method that I used in the getRandomChar() function. Created a table that has the numbers you want to use, then use math.random() to pick a random entry from that table:

local function getRandomChar() local validChars = {3, 5, 7} --#validChars will give us the length of this table, i.e. the number of items in this table, which is currently 3 return validChars[math.random(1, #validChars)] end local myRandomChar = getRandomChar()

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()

Thanks, that explains a lot. 

Another similar query, math.random gives us one number from a range of numbers. What should I do if I want it to return one number out of, say, 3,5 and 7? 

Also, if I want to get any number in the range 1-20 EXCEPT 15, how can I do that?

thanks.

If you want to pick a number from a limited selection, just use the exact same method that I used in the getRandomChar() function. Created a table that has the numbers you want to use, then use math.random() to pick a random entry from that table:

local function getRandomChar() local validChars = {3, 5, 7} --#validChars will give us the length of this table, i.e. the number of items in this table, which is currently 3 return validChars[math.random(1, #validChars)] end local myRandomChar = getRandomChar()