# Untitled

By: a guest on May 4th, 2012  |  syntax: Lua  |  size: 2.25 KB  |  hits: 17  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. function GenerateQuestion()
2.         local numbers = 3 -- Amount of numbers the question will have.
3.         local numberMin = -20 -- Lower limit for the number randomization.
4.         local numberMax = 20 -- Upper limit for the number randomization.
5.         local asterisks = 0 -- Number of multiplication operators (a chunk later on ensures that there's only one multiplication).
6.         local signs = {'+', '-', '*'} -- Available operators.
7.         question = "" -- The question which the function will output.
8.
9.         for i = 1, numbers, 1 do -- Generates random numbers and formats them into the question string.
10.                 local number = tostring(math.random(numberMin, numberMax)) -- Generates a random number within the specified limits.
11.
12.                 if i < numbers then -- Ensures that an operator is only placed after a number if the number is not the last in the sequence.
13.                         local sign = signs[math.random(1, #signs)] -- Randomly picks an operator.
14.                         if sign == '*' then
15.                                 asterisks = asterisks + 1 -- Increments the number of multiplication operators.
16.                         end
17.                         if tonumber(number) < 0 then
18.                                 number = "("..number..")" -- Adds parentheses around a number if it's negative.
19.                         end
20.                         question = question..number.." "..sign.." " -- Concatenates the number and sign onto the question string.
21.
22.                 else -- Gets called once the loop reaches the last number, and doesn't place an operator after the number.
23.                         if tonumber(number) < 0 then
24.                                 number = "("..number..")" -- Adds parentheses around a number if it's negative.
25.                         end
26.                         question = question..number -- Concatenates the number onto the question string.
27.                 end
28.         end
29.
30.         if asterisks > 1 then
31.                         question = question:gsub('*', signs[math.random(1, #signs - 1)], numbers - 2) -- Ensures that there's only one multiplication.
32.         end
33.
34.         return question -- Returns the final question string.
35. end
36.
37. function EvaluateQuestion()
38.         local s = "solution = "..question -- Concatenates the question string onto an assignment syntax.
39.         RunString(s) -- Parses the code contained in s, and calculates the value of the solution variable.
40.         return solution -- Returns the solution to the question.
41. end
42.
43. function PrintQuestion()
44.         print(GenerateQuestion())
45.         print(EvaluateQuestion())
46. end
47.
48. timer.Create("timer", 120, 0, PrintQuestion)