[SOLVED]Surely there's a better way of storing[...]

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
ntpspe
Prole
Posts: 31
Joined: Sun Aug 07, 2011 6:19 pm

[SOLVED]Surely there's a better way of storing[...]

Post by ntpspe » Wed Aug 10, 2011 10:23 pm

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 :)
Last edited by ntpspe on Thu Aug 11, 2011 12:03 am, edited 1 time in total.

User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: Surely there's a better way of storing my random variabl

Post by TechnoCat » Wed Aug 10, 2011 10:43 pm

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]

User avatar
ntpspe
Prole
Posts: 31
Joined: Sun Aug 07, 2011 6:19 pm

Re: Surely there's a better way of storing my random variabl

Post by ntpspe » Wed Aug 10, 2011 10:58 pm

Haha definately a better method :P 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

User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Surely there's a better way of storing my random variabl

Post by tentus » Wed Aug 10, 2011 11:04 pm

"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).
Kurosuke needs beta testers

User avatar
ntpspe
Prole
Posts: 31
Joined: Sun Aug 07, 2011 6:19 pm

Re: Surely there's a better way of storing my random variabl

Post by ntpspe » Wed Aug 10, 2011 11:09 pm

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?

User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: Surely there's a better way of storing my random variabl

Post by TechnoCat » Wed Aug 10, 2011 11:19 pm

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.

User avatar
ntpspe
Prole
Posts: 31
Joined: Sun Aug 07, 2011 6:19 pm

Re: Surely there's a better way of storing my random variabl

Post by ntpspe » Wed Aug 10, 2011 11:36 pm

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 :D

User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: Surely there's a better way of storing my random variabl

Post by TechnoCat » Wed Aug 10, 2011 11:38 pm

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 :D
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

User avatar
ntpspe
Prole
Posts: 31
Joined: Sun Aug 07, 2011 6:19 pm

Re: Surely there's a better way of storing my random variabl

Post by ntpspe » Wed Aug 10, 2011 11:55 pm

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

User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: Surely there's a better way of storing my random variabl

Post by TechnoCat » Thu Aug 11, 2011 12:00 am

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 36 guests