Randomizing something other than XY coordinates

I found the info about math.random but I’m not clear if there’s a way with Lua to randomize the arrays associated with a Table.

I read that  arrays  "can be indexed not only with numbers, but with any value (except nil).

Is that what we would use to apply math.random to have a random question/clue/audio file/etc. (within a table) shown or played?

If so, how do you “index” the questions/clues/audio files  with numbers for math.random to work.

Am I even understanding this correctly?   :rolleyes:

There are two types of tables:  indexed with a number (Arrays) and indexed with a key (Key-Value pairs).  Randomizing key-value pairs makes no sense.  There is no implied order. You get the key when you get the key.   However when indexing using numbers, being able to access those values randomly is quiet common.

There are two ways to access table data randomly:  1. Where any table member can come up multiple times or 2. Where each value gets used only once.  This second mode is known as shuffling.

In the first mode, you can just generate a random number between 1 and the table size and use that value to reference the table entry:

local myTable = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }

local randomEntry = myTable[math.random( #myTable )]

However, many people only want to use a value one.  To do that you shuffle the table (like a deck of cards):

local function shuffleTable(t)
    local rand = math.random
    assert(t, “table.shuffle() expected a table, got nil”)
    local iterations = #t
    local j
    
    for i = iterations, 2, -1 do
        j = rand(i)
        t[i], t[j] = t[j], t[i]
    end
end

local myTable = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }

local shuffledTable = shuffleTable( myTable )

Then you use each entry one at a time

local randomEntry = shuffledTable[1]

Rob

Rob wrote:
In the first mode, you can just generate a random number between 1 and the table size and use that value to reference the table entry:

Thank you for the explanation, Rob. Your help is much appreciated!

Using the first example you describe above of random, when I substitute the table entries with numbers, I get errors. See below.

I started with this:

local soundTable = {
soundC = audio.loadSound( “audio/c.aac” ),
soundA = audio.loadSound( “audio/a.aac” ),
soundG = audio.loadSound( “audio/g.aac” ),
soundT = audio.loadSound( “audio/t.aac” ),
soundU = audio.loadSound( “audio/u.aac” )
}

If I replace the “soundC” with “1”, I get this error :
’}’ expected (to close ‘{’ at line 25) near '='

(my line 25 is local soundTable = { )

Is this what you mean "use that numerical value to reference the table entry" in this case called soundC?

My other question using your 2nd example of shuffling: are all numbers 1-10 used only one time before a number is repeated? If so, then does this random sequence go on and on until the user quits or something else ends this random sequence?

What you’ve done here:

local soundTable = {     soundC = audio.loadSound( "audio/c.aac" ),     soundA = audio.loadSound( "audio/a.aac" ),     soundG = audio.loadSound( "audio/g.aac" ),     soundT = audio.loadSound( "audio/t.aac" ),     soundU = audio.loadSound( "audio/u.aac" ) }

is create a "Key-Value pair table.  Your key is soundC, soundA, etc.  The values are the sounds that you load.

To do a numeric table (i.e. array), you would do:

local soundTable = {} soundTable[1] = audio.loadSound( "audio/c.aac" ) soundTable[2] = audio.loadSound( "audio/a.aac" ) soundTable[3] = audio.loadSound( "audio/g.aac" ) soundTable[4] = audio.loadSound( "audio/t.aac" ) soundTable[5] = audio.loadSound( "audio/u.aac" )

Then soundTable[math.random(#soundTable)] should work for you.   Now you can do a hybrid version that uses two tables, one numeric indexed that holds the key’s of the key-value table:

local soundKeys = { "soundC", "soundA", "soundG", "soundT", "soundU" } local soundTable = {     soundC = audio.loadSound( "audio/c.aac" ),     soundA = audio.loadSound( "audio/a.aac" ),     soundG = audio.loadSound( "audio/g.aac" ),     soundT = audio.loadSound( "audio/t.aac" ),     soundU = audio.loadSound( "audio/u.aac" ) }   audio.play( soundTable[soundKeys[ math.random(#soundKeys)] ] )

In this example, we are going to generate a random number and look up the key that we then use to look up the actual value.

In the 2nd shuffle question, once you consume all the entries in the table,  you have to reshuffle it again.

Thank you, Rob!  Works like a charm. 

There are two types of tables:  indexed with a number (Arrays) and indexed with a key (Key-Value pairs).  Randomizing key-value pairs makes no sense.  There is no implied order. You get the key when you get the key.   However when indexing using numbers, being able to access those values randomly is quiet common.

There are two ways to access table data randomly:  1. Where any table member can come up multiple times or 2. Where each value gets used only once.  This second mode is known as shuffling.

In the first mode, you can just generate a random number between 1 and the table size and use that value to reference the table entry:

local myTable = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }

local randomEntry = myTable[math.random( #myTable )]

However, many people only want to use a value one.  To do that you shuffle the table (like a deck of cards):

local function shuffleTable(t)
    local rand = math.random
    assert(t, “table.shuffle() expected a table, got nil”)
    local iterations = #t
    local j
    
    for i = iterations, 2, -1 do
        j = rand(i)
        t[i], t[j] = t[j], t[i]
    end
end

local myTable = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }

local shuffledTable = shuffleTable( myTable )

Then you use each entry one at a time

local randomEntry = shuffledTable[1]

Rob

Rob wrote:
In the first mode, you can just generate a random number between 1 and the table size and use that value to reference the table entry:

Thank you for the explanation, Rob. Your help is much appreciated!

Using the first example you describe above of random, when I substitute the table entries with numbers, I get errors. See below.

I started with this:

local soundTable = {
soundC = audio.loadSound( “audio/c.aac” ),
soundA = audio.loadSound( “audio/a.aac” ),
soundG = audio.loadSound( “audio/g.aac” ),
soundT = audio.loadSound( “audio/t.aac” ),
soundU = audio.loadSound( “audio/u.aac” )
}

If I replace the “soundC” with “1”, I get this error :
’}’ expected (to close ‘{’ at line 25) near '='

(my line 25 is local soundTable = { )

Is this what you mean "use that numerical value to reference the table entry" in this case called soundC?

My other question using your 2nd example of shuffling: are all numbers 1-10 used only one time before a number is repeated? If so, then does this random sequence go on and on until the user quits or something else ends this random sequence?

What you’ve done here:

local soundTable = {     soundC = audio.loadSound( "audio/c.aac" ),     soundA = audio.loadSound( "audio/a.aac" ),     soundG = audio.loadSound( "audio/g.aac" ),     soundT = audio.loadSound( "audio/t.aac" ),     soundU = audio.loadSound( "audio/u.aac" ) }

is create a "Key-Value pair table.  Your key is soundC, soundA, etc.  The values are the sounds that you load.

To do a numeric table (i.e. array), you would do:

local soundTable = {} soundTable[1] = audio.loadSound( "audio/c.aac" ) soundTable[2] = audio.loadSound( "audio/a.aac" ) soundTable[3] = audio.loadSound( "audio/g.aac" ) soundTable[4] = audio.loadSound( "audio/t.aac" ) soundTable[5] = audio.loadSound( "audio/u.aac" )

Then soundTable[math.random(#soundTable)] should work for you.   Now you can do a hybrid version that uses two tables, one numeric indexed that holds the key’s of the key-value table:

local soundKeys = { "soundC", "soundA", "soundG", "soundT", "soundU" } local soundTable = {     soundC = audio.loadSound( "audio/c.aac" ),     soundA = audio.loadSound( "audio/a.aac" ),     soundG = audio.loadSound( "audio/g.aac" ),     soundT = audio.loadSound( "audio/t.aac" ),     soundU = audio.loadSound( "audio/u.aac" ) }   audio.play( soundTable[soundKeys[ math.random(#soundKeys)] ] )

In this example, we are going to generate a random number and look up the key that we then use to look up the actual value.

In the 2nd shuffle question, once you consume all the entries in the table,  you have to reshuffle it again.

Thank you, Rob!  Works like a charm.