generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Help with tables

Previous Thread :: Next Thread 
swimguy777 is not online. swimguy777
Joined: 30 May 2009
Total Posts: 17092
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 is not online. 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
swimguy777 is not online. swimguy777
Joined: 30 May 2009
Total Posts: 17092
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
swimguy777 is not online. swimguy777
Joined: 30 May 2009
Total Posts: 17092
09 May 2012 06:37 PM
Halp

-[::ƧѡÎḾḠΰῩ::]-[::Maker of stuff and Helper of Scripting::]-
Report Abuse
smurf279 is not online. 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 is not online. 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 is not online. 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
swimguy777 is not online. swimguy777
Joined: 30 May 2009
Total Posts: 17092
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
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
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
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
09 May 2012 08:51 PM
Boo.


if not (type(_)=="number") then




Derp. >_>
Report Abuse
smurf279 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
09 May 2012 09:44 PM
^
*Looks at my script*
Cheater :P
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
09 May 2012 09:59 PM
I didn't understand your script.
Until I just looked at it again. >_<
Report Abuse
smurf279 is not online. 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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image