As for randomisation, here's a much better, slightly different version I made. See what you can understand of it yet, it'll always be here for future reference
It randomises by first creating a table with 100 "random" numbers in it (using the math.random() function which isn't good at all), then getting the millisecond value of the current time and using it to select one of the 100 random numbers. It would have been easier to use seconds, but if you want to use this repeatedly it is better to use milliseconds as computers can do more than one thing in a second
+Rep to anyone who can point out the flaw in that combination of numbers + milliseconds
(Yes, I am aware it is there
)
Code:
--os.clock() returns the time in seconds that Lua has been running for. It is the only Lua
--clock function that returns a time value in seconds and milliseconds, making it very useful.
--math.modf() returns the whole and fractional values of the number handed to it - using this
--we can separate the seconds from the milliseconds
function TCS_Rand_Num ()
--Assign the os.clock output to a var
local DateAndTimeRandNum = os.clock()
--Assign the separated value to a var. (NOTE: This will only return the first of two values,
--so the whole number part, not the fractional.
local Num_1 = math.modf(DateAndTimeRandNum)
--declare a table
local Rand_Num_table = {}
--for 1 -> 100 assign the ith value to be a pseudo-random number, using math.random
for i = 1, 100 do
Rand_Num_table[i] = math.random(1, 100)
end
--take the clock value (seconds and milliseconds) away from the separated value(seconds) to
--leave you with only a milliseconds value. then multiply it by 100 and round up to the nearest integer (math.ceil)
--to give a whole number based on the millisecond the function was called.
local Num_2 = (math.ceil((DateAndTimeRandNum - Num_1) * 100))
--then using the number select one of the pseudo-random numbers from the table "Rand_Num_table"
--and return it
return Rand_Num_table[Num_2]
end
So you can use this as follows:
Code:
local NumberVar = TCS_Rand_Num ()
if NumberVar > 3 then
...
end