|
| 09 May 2012 06:00 PM |
How do I pick a random element out of a table with non-numerical indices?
-[::ƧѡÎḾḠΰῩ::]-[::Maker of stuff and Helper of Scripting::]- |
|
|
| Report Abuse |
|
|
Aerideyn
|
  |
| Joined: 16 Jan 2010 |
| Total Posts: 1882 |
|
|
| 09 May 2012 06:05 PM |
a for in pairs loop will work with non numerical indices, so just set a random chance to choose one entry and loop until one is chosen.
element = nil
while not element do -- while loop is incase one doesnt get chosen - try again. for i,v in pairs(table) do if math.Random(1,#table) == 1 then -- 1 in #table chance of being chosen. element = v end end end |
|
|
| Report Abuse |
|
|
|
| 09 May 2012 06:15 PM |
Except for one small problem:
t = {A = "A", B = "B"} print(#t)
>0
-[::ƧѡÎḾḠΰῩ::]-[::Maker of stuff and Helper of Scripting::]- |
|
|
| Report Abuse |
|
|
|
| 09 May 2012 06:37 PM |
Halp
-[::ƧѡÎḾḠΰῩ::]-[::Maker of stuff and Helper of Scripting::]- |
|
|
| Report Abuse |
|
|
smurf279
|
  |
| Joined: 15 Mar 2010 |
| Total Posts: 6871 |
|
|
| 09 May 2012 06:48 PM |
your_table[(function(table) local t = {} for i in next, table do t[#t+1] = i end return t[math.random(#t)] end)(your_table)] |
|
|
| Report Abuse |
|
|
kools
|
  |
| Joined: 11 Jan 2009 |
| Total Posts: 1659 |
|
|
| 09 May 2012 07:05 PM |
This is more simple than it seems.
Table = {"A", "B", "C", "D"} print(Table[math.random(1, #Table)])
I think ... |
|
|
| Report Abuse |
|
|
Aerideyn
|
  |
| Joined: 16 Jan 2010 |
| Total Posts: 1882 |
|
|
| 09 May 2012 07:22 PM |
alright, so # table will return 0, then just choose an arbitrary number for the upper bounds of the math.random.. it really doesnt matter. you will find for i,v in pairs tends to mix it all up anyway so you could even just try returning the first element though adding a random function will make it less predictable. |
|
|
| Report Abuse |
|
|
|
| 09 May 2012 07:38 PM |
I figured something out, it's not pretty, nor is it particularly efficient, but it gets the job done:
t = {A = "A", B = "B", C = "C"} num = 0
for i,v in pairs(t) do num = num + 1 end
rand = math.random(1,num) num = 0
for i,v in pairs(t) do num = num + 1 if num == rand then print(i,v) end end
-[::ƧѡÎḾḠΰῩ::]-[::Maker of stuff and Helper of Scripting::]- |
|
|
| Report Abuse |
|
|
|
| 09 May 2012 08:49 PM |
randomDictionary = function(dictionary) local r = {} for _, v in pairs(dictionary) do if not type(_)=="number" then table.insert(r, _) end end return dictionary[r[math.random(1, #r)]] end
local t = { A = "A", B = "B", C = "C", D = "D" }
for i = 1, 4 do print( randomDictionary(t) ) end |
|
|
| Report Abuse |
|
|
|
| 09 May 2012 08:51 PM |
Boo.
if not (type(_)=="number") then
Derp. >_> |
|
|
| Report Abuse |
|
|
smurf279
|
  |
| Joined: 15 Mar 2010 |
| Total Posts: 6871 |
|
|
| 09 May 2012 09:44 PM |
^ *Looks at my script* Cheater :P |
|
|
| Report Abuse |
|
|
|
| 09 May 2012 09:59 PM |
I didn't understand your script. Until I just looked at it again. >_<
|
|
|
| Report Abuse |
|
|
smurf279
|
  |
| Joined: 15 Mar 2010 |
| Total Posts: 6871 |
|
|
| 09 May 2012 10:04 PM |
*cough cough* liar *cough cough* :P
nah its alright |
|
|
| Report Abuse |
|
|