Skip to main content
Fixed code formatting. Note that 'indexes' is correct here as a present-tense verb: http://conjugator.reverso.net/conjugation-english-verb-indexes.html
Source Link
BSMP
  • 4.7k
  • 8
  • 34
  • 45

Lua indexes tables from 1, unlike C, Java etc. which indexes arrays from 0. That means, that in your table, the valid indexes are: 1, 2, 3, 4. What you are looking for is the following:

print( myTable[ math.random( #myTable ) ] )

print( myTable[ math.random( #myTable ) ] )

When called with one argument, math.random(n) returns a random integer from 1 to n including.

Lua indexes tables from 1, unlike C, Java etc. which indexes arrays from 0. That means, that in your table, the valid indexes are: 1, 2, 3, 4. What you are looking for is the following:

print( myTable[ math.random( #myTable ) ] )

When called with one argument, math.random(n) returns a random integer from 1 to n including.

Lua indexes tables from 1, unlike C, Java etc. which indexes arrays from 0. That means, that in your table, the valid indexes are: 1, 2, 3, 4. What you are looking for is the following:

print( myTable[ math.random( #myTable ) ] )

When called with one argument, math.random(n) returns a random integer from 1 to n including.

Source Link
Michal Kottman
  • 16.7k
  • 3
  • 49
  • 62

Lua indexes tables from 1, unlike C, Java etc. which indexes arrays from 0. That means, that in your table, the valid indexes are: 1, 2, 3, 4. What you are looking for is the following:

print( myTable[ math.random( #myTable ) ] )

When called with one argument, math.random(n) returns a random integer from 1 to n including.