Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

math.random with Tables Dictionary?

Asked by 7 years ago

local Types = { ["LeftPunch"] = {Id = 1784490345, DamagePart = "LeftHand", Damage = 10}, ["RightPunch"] = {Id = 1784483797, DamagePart = "RightHand", Damage = 10} }

local Type = Types[math.random(1, #table)] print(Type)

it keep saying interval is empty

0
#Types instead of #table? User#20279 0 — 7y
0
nope MaxOutGaming -3 — 7y
0
You don't have a table named "table" and you can't anyways. MooMooThalahlah 421 — 7y

3 answers

Log in to vote
2
Answered by 7 years ago
Edited 7 years ago

I did some reading up on why you can't print(Types[1]). Or print(#Types) keeps returning 0. Here's a quote from:

lua.users.org

The # operator doesn't count all the items in the table (!). Instead it finds the last integer (non-fractional number) key. Because of how it's implemented its results are undefined if all the integer keys in the table aren't consecutive. Which is why it shouldn't be used for tables used as sparse arrays[2]).

If you go to the website and click the "2" it will lead you to Wikipedia and you can read more about it.

Interval is empty because you tried to do math.random(1,0)

Not sure if this is the best approach but you can explicitly assign an Index value in your tables.

01-- Assign your own index
02local Types = {
03    ["LeftPunch"] = {Index = 1, Id = 1784490345, DamagePart = "LeftHand", Damage = 10},
04    ["RightPunch"] = {Index = 2, Id = 1784483797, DamagePart = "RightHand", Damage = 10}
05}
06local numOfPunches = 0
07 
08print("What is #table: " .. #table)
09--print(math.random(1,0)) -- Interval is empty
10--print(math.random(1,#table)) -- Interval is empty
11 
12print(#Types) -- Prints 0
13-- Not what we wanted
14print(Types[1]) -- Prints nil
15-- Also not what we wanted
View all 38 lines...
Ad
Log in to vote
0
Answered by
hellmatic 1523 Moderation Voter
7 years ago
Edited 7 years ago
01local Types = {
02    Enabled = true
03 
04    ,RightPunch = {
05        Enabled = true
06 
07        ,Damage = 10
08 
09        ,Id = 1784483797
10        ,Hand = "RightHand"
11     }
12 
13    ,LeftPunch = {
14        Enabled = true
15 
View all 42 lines...
Log in to vote
0
Answered by
Validark 1580 Snack Break Moderation Voter
7 years ago

Here is a generalized WeightedProbability Generator function. It is also installable through the RoStrap plugin. Documentation available here.

01local WeightedProbabilities = {}
02 
03local WeightedProbabilityFunction =  {
04    -- Generates option-picker functions from relative probabilities
06    -- @rostrap WeightedProbabilityFunction
07    -- @author Validark
08 
09    new = function(ProbabilityData)
10        -- @param dictionary in the form: {
11        --      [variant option1] = number Weight1;
12        --      [variant option2] = number Weight2;
13        -- }
14        -- @returns a function that, when called, selects an option from its probability and returns it
15 
View all 77 lines...

Answer this question