Ok, I’ve put this aside for a bit and got a copy of Programming in Lua so I could read up on things.
I’ll try that, thanks.
I read that, can’t recall where, but that it’s faster and uses less memory to do this(table inserts in general)
[lua]
local mRand = math.random
local count = mRand(3,7)
local t1 = { {“a”, 1}, {“o”, 1}, {“p”, 2}, {“v”, 2}, {“i”, 1}, {“c”, 5}, {“b”, 1}, {“x”,9}, {“w”, 1}, {“m”, 2}, {“e”, 3} }
local t2 = {}
for i = 1, count do
local val = mRand(1, #t1)
t2[#t2 + 1] = t1[val]
end
print_r(t2)
[/lua]
another test…
[lua]
local mRand = math.random
local count = mRand(3,7)
local t1 = { {“a”, 1}, {“o”, 1}, {“p”, 2}, {“v”, 2}, {“i”, 1}, {“c”, 5}, {“b”, 1}, {“x”,9}, {“w”, 1}, {“m”, 2}, {“e”, 3} }
local t2 = {}
local val = mRand(1, #t1)
for i = 1, count do
t2[#t2 + 1] = t1[val]
end
print_r(t2)
[/lua]
another test…
[lua]
local mRand = math.random
local count = mRand(3,7)
local t1 = { {“a”, 1}, {“o”, 1}, {“p”, 2}, {“v”, 2}, {“i”, 1}, {“c”, 5}, {“b”, 1}, {“x”,9}, {“w”, 1}, {“m”, 2}, {“e”, 3} }
local t2 = {}
for i = 1, count do
t2[#t2 + 1] = t1[mRand(1, #t1)]
end
print_r(t2)
[/lua]
Though I noticed that sometimes a few entries are empty in my print_r( ), the empty ones show
[4] => table: 0x10d4c34f0 {*table =0x112d23b60} etc.
It inserts a table but not the values inside, how come? is this method too fast? where did I go wrong with my randomization???
this btw doesn’t give empty entries, however I do want to make the random thing work for my own sanity…
[lua]
local mRand = math.random
local count = mRand(3,7)
local t1 = { {“a”, 1}, {“o”, 1}, {“p”, 2}, {“v”, 2}, {“i”, 1}, {“c”, 5}, {“b”, 1}, {“x”,9}, {“w”, 1}, {“m”, 2}, {“e”, 3} }
local t2 = {}
for i = 1, count do
t2[#t2 + 1] = t1[i]
end
print_r(t2)
[/lua]
Thanks for the help