Page 1 of 2
[SOLVED]Surely there's a better way of storing[...]
Posted: Wed Aug 10, 2011 10:23 pm
by ntpspe
Hey guys, I've managed to create 21 variables that are different each time my game is loaded, which is perfect sure, but the problem lies here.
This is how i've stored them 21 variables (and aquired them)
Code: Select all
math.randomseed(os.time())
aa = math.random(0,2)
ab = math.random(0,2)
ac = math.random(0,2)
ad = math.random(0,2)
ae = math.random(0,2)
af = math.random(0,2)
ag = math.random(0,2)
ah = math.random(0,2)
ai = math.random(0,2)
aj = math.random(0,2)
ak = math.random(0,2)
al = math.random(0,2)
am = math.random(0,2)
an = math.random(0,2)
ao = math.random(0,2)
ap = math.random(0,2)
aq = math.random(0,2)
ar = math.random(0,2)
as = math.random(0,2)
at = math.random(0,2)
au = math.random(0,2)
and as you can see it's a very long-winded method, and to be honest, I need 410 variables for the example, and probably thousands more for the real thing.
So, as the title says, surely there's a better way of storing my random variables?
Thanks in advance
Re: Surely there's a better way of storing my random variabl
Posted: Wed Aug 10, 2011 10:43 pm
by TechnoCat
Code: Select all
math.randomseed(os.time())
randoms = {}
for i=1, 420 do
table.insert(randoms,math.random(0,2))
end
Access them with randoms[1], randoms[2], ... randoms[n]
Re: Surely there's a better way of storing my random variabl
Posted: Wed Aug 10, 2011 10:58 pm
by ntpspe
Haha definately a better method
Thank you kind sir!
As another request, could you please explain this part:
Code: Select all
for i=1, 420 do
table.insert(randoms,math.random(0,2))
I know it's probably simple, but I've been having trouble with 'for' statements
Re: Surely there's a better way of storing my random variabl
Posted: Wed Aug 10, 2011 11:04 pm
by tentus
"for" is a loop. Whatever is inside it will be executed each time the loop occurs (in this case, 420 times). The difference each time will be the "i" variable, which will increase by one each time the loop occurs.
The "table.insert" function inserts the second parameter into the first parameter (in this case, a table called random).
Re: Surely there's a better way of storing my random variabl
Posted: Wed Aug 10, 2011 11:09 pm
by ntpspe
Aah I understand now, thanks. So is it fair to say 'for' is similar to 'while' in a way? (of course 'while' only does it whilst the parameters are met).
Now onto a more complex thinymajig, is there a way to make is so one of the 3 numbers (0,1,2) is more likely to appear? and when I add more numbers?
Re: Surely there's a better way of storing my random variabl
Posted: Wed Aug 10, 2011 11:19 pm
by TechnoCat
ntpspe wrote:Now onto a more complex thinymajig, is there a way to make is so one of the 3 numbers (0,1,2) is more likely to appear?
Sure
if you want 30% 0, 60% 1 and 10% 2:
Code: Select all
random_table = {}
for i=1, 420 do
local random = math.random()
if random<=0.3 then
random = 0
elseif random<=0.9 then
random = 1
else
random = 2
end
table.insert(random_table,random)
end
Note: 0.3+0.6+0.1 = 1. Your total probability will almost always equal 1.
Re: Surely there's a better way of storing my random variabl
Posted: Wed Aug 10, 2011 11:36 pm
by ntpspe
elseif random<=0.9 then
Was that meant to be 0.6? And aah thanks a million, you've really helped me out guys! Thanks
Re: Surely there's a better way of storing my random variabl
Posted: Wed Aug 10, 2011 11:38 pm
by TechnoCat
ntpspe wrote:elseif random<=0.9 then
Was that meant to be 0.6? And aah thanks a million, you've really helped me out guys! Thanks
No it is not.
It works like this: (0 0.3] is 30%, (0.3 0.9] is 60%, (0.9 1.0] is 10%
interval notation
Re: Surely there's a better way of storing my random variabl
Posted: Wed Aug 10, 2011 11:55 pm
by ntpspe
(0 0.3] is 30%, (0.3 0.9] is 60%, (0.9 1.0] is 10%
Aah, that's quite complex (as much as I can understand haha) but is it somewhat along the lines of:
0 TO 0.3 is 30%
0.3 TO 0.9 is 60% (0.9-0.3=0.6)
0.9 TO 1.0 is 10% (1.0-0.9=0.1)
Something like that?
So if I had 5 variables, rather than the three currently, would it be
0 TO 0.2 is 20%
0.2 TO 0.4 is 20%
0.4 TO 0.6 is 20%
0.6 TO 0.8 is 20%
0.8 TO 1.0 is 20%
Like that?
(Please let me be right, it makes sense to me lmao)
Re: Surely there's a better way of storing my random variabl
Posted: Thu Aug 11, 2011 12:00 am
by TechnoCat
ntpspe wrote:(0 0.3] is 30%, (0.3 0.9] is 60%, (0.9 1.0] is 10%
Aah, that's quite complex (as much as I can understand haha) but is it somewhat along the lines of:
0 TO 0.3 is 30%
0.3 TO 0.9 is 60% (0.9-0.3=0.6)
0.9 TO 1.0 is 10% (1.0-0.9=0.1)
Something like that?
So if I had 5 variables, rather than the three currently, would it be
0 TO 0.2 is 20%
0.2 TO 0.4 is 20%
0.4 TO 0.6 is 20%
0.6 TO 0.8 is 20%
0.8 TO 1.0 is 20%
Like that?
(Please let me be right, it makes sense to me lmao)
Yes, that is correct. That would be for 5 equally probable events. They are all 20% likely.