math.random... Tables ..is it possible ?

Hi
Is it possible to pick up all positions from table this way ? One by one
Exmple…

numbers = {1,2,3,4,5,6,7,8,9,10}  
  
for i =1 , #numbers do  
random = numbers[math.random(1, #numbers)  
end  
  

This code can pick up for example number 10 and that’s it.
How to pickup ALL numbers from table in random order? [import]uid: 13156 topic_id: 13541 reply_id: 313541[/import]

You could implement some sort of shuffle then just run through it

local numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}  
  
numbers = ShuffleFunction(numbers)  
  
for i = 1, #numbers do  
 random = numbers[index]  
end  

or if you don’t mind destroying your table you could remove used entries. Something like

local numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}  
local size\_of\_table = #numbers  
  
for i = 1, size\_of\_table do  
 local index = math.random(#numbers)  
 random = numbers[index]  
 table.remove(numbers, index)  
end  

this is just off the top of my head so there might be a better way of doing it. [import]uid: 68937 topic_id: 13541 reply_id: 49749[/import]

mattthew maybe I did something wrong but the result is the same :slight_smile:

I need to pick up ALL object from table one by one on random order. [import]uid: 13156 topic_id: 13541 reply_id: 49753[/import]

matthew sorry I was wrong. Thank you math.random on table indexes is a good idea. [import]uid: 13156 topic_id: 13541 reply_id: 49761[/import]