Table.Random failing on unordered tables

Issue #27 wontfix
Fabian Maurer created an issue

Code:

local tab = {};
--tab[1] = "hi2";
tab[2] = "hi";
rnd = Table.Random(tab);
say rnd;

This always crashes, because "rnd" is nil. It seems that "Table.Random" only works if the table is ordered. If you were to turn the commented line into actual code, it would work fine.

Comments (2)

  1. Chimaine

    This is the intended behavior.

    Table.Random works on the list part of the table. This is also mentioned in the documentation in the wiki on that function.

    //edit: To clarify, the list part of the table is defined as follows in the language specification:

    Tables have an implicit sequence part, that is, the set of its positive numeric keys is equal to {1..n} for some integer n.

  2. Chimaine

    If adding/removing to the list/sequence is a problem, remember that this is what the Table.Insert and Table.Remove functions are for. Find me on IRC tomorrow.

  3. Log in to comment