 # Table random

How to random stuff inside a table?

my current code:

``````

randi {
"hello1",
"hello2",
"hello3",
"hello4",
"hello5"
}

randithing = {
["thing1"] = {math.Random(randi)},
["thing2"] = {math.Random(randi)},
["thing3"] = {math.Random(randi)},
["thing4"] = {math.Random(randi)},
["thing5"] = {math.Random(randi)}
}

``````

Do you want to fill the table randithing with random entries from randi? If so, you can do this:

[lua]randi {
“hello1”,
“hello2”,
“hello3”,
“hello4”,
“hello5”
}

randithing = {
thing1 = table.Random(randi),
thing2 = table.Random(randi),
thing3 = table.Random(randi),
thing4 = table.Random(randi),
thing5 = table.Random(randi)
}

[/lua]

or better

[lua]
randi {
“hello1”,
“hello2”,
“hello3”,
“hello4”,
“hello5”
}

randithing = {}

for i=1,5 do
randithing[“thing”…i] = table.Random(randi)
end
[/lua]

Thank you!
But is table.random Garrys mod only?

Yes, I am pretty sure it is. At least I couldn’t find it in the reference manual.

Darn! Is there any other way to make it without table.random then?

Sure:

[lua]
randi {
“hello1”,
“hello2”,
“hello3”,
“hello4”,
“hello5”
}

randithing = {}

for i=1,5 do
randithing[“thing”…i] = randi[math.random(1,#randi)]
end
[/lua]

I simply used table.Random for convenience.

doesn’t compile current code now:

``````

randi {
"hello1",
"hello2",
"hello3",
"hello4",
"hello5"
}

randithing = {
["thing1"] = randi[math.random(1,#randi)],
["thing2"] = randi[math.random(1,#randi)],
["thing3"] = randi[math.random(1,#randi)],
["thing4"] = randi[math.random(1,#randi)],
["thing5"] = randi[math.random(1,#randi)]
}

``````

also tried:

``````

randi {
"hello1",
"hello2",
"hello3",
"hello4",
"hello5"
}

randithing = {
["thing1"] = get_testi(),
["thing2"] = get_testi(),
["thing3"] = get_testi(),
["thing4"] = get_testi(),
["thing5"] = get_testi()
}

function get_testi()
th = math.random(1,#randi)
tama = randi[th]
return tama
end

``````

btw those [" "] are necessary.

Doing this:

[lua] table = { [“thing1”] = “something”}[/lua]

is the same thing as this:

[lua] table = {thing1 = “something”}[/lua]

Also, you were missing a ‘=’ in the first line (randi = {…}), sorry about that.

Here is my output

``````

thing3    =    hello5
thing4    =    hello2
thing5    =    hello1
thing2    =    hello4
thing1    =    hello3

``````

– Snip this part –

From what I can see, all module extension like math and table that start with a Capital letter(table.GetFirstKey) are Gmod only… I could be wrong though.
function below is a table.random function you can use in normal lua.
It supports index’s that are of all types.

[lua]
function table.random(tbl)
local oldtab = {}
for k,v in pairs(tbl) do
table.insert(oldtab, k)
end
return tbl[oldtab[math.random(1, #oldtab)]]
end
– This supports non-numerical indexs if your wondering why I didn’t just
– return tbl[math.random(1, #tbl)]
[/lua]

Looks like that the board i am compiling this don’t know what math.random is.

``````

randithing = {
thing = math.Random(1, 10)
}

``````

never compiles

because lua is case-sensitive: Use math.random.

typo. changed to math.random, still fails to compile.

There is nothing wrong with the code you posted. And if you say it does not compile, post the error it spews.

It works fine, tried it here http://www.lua.org/cgi-bin/demo your compiler must suck?..

Yes, it sucks. I think i have to write my own random system by c++ somehow…