Choose a random item from a table

后端 未结 6 1693
既然无缘
既然无缘 2021-02-03 20:52

My goal is to pick out a random item from a table in Lua.

This is what I\'ve got so far, but it currently does not work:

local myTable = { \'a\', \'b\',          


        
6条回答
  •  北海茫月
    2021-02-03 21:10

    table = {'Apple', 'Banana', 'Orange', 'Watermelon' , 'lychee'} --my table have 5 item.
    
    rand= math.random(1~5) --create a random number numbers 1 to 5 if more than 5 then the value is nil.
    
    print(rand)
    print(table[rand]) --unite the random number and item it will display 5 random items in the table.
    

提交回复
热议问题