# How can I get 10 random values out of a table?

How can I get 10 random values out of a table?

1 Like

forgot how tables worked but if i remember correctly why not just a loop with “table[math.random(num1, num2)]”?

then again i might be goign stupid here

oh phew so im not really going stupid

Perform a for loop and get random values out of a table with math.random(1, #table)

Something like this?

``````local tab = {142,51,61,67,72,992,82,2,8,145,636,163,63}
local tab1 = {}

for i = 1,10 do
table.insert(tab1,tab[math.random(1,#tab)])
end

print(#tab1)
``````

So something like this?

``````	local Queued = 1
repeat
table.find(PlayersInQueue, math.random(1, #PlayersInQueue))
Queued += 1
until Queued == 20
``````
1 Like

Yo why not just use Queued += 1 im pretty sure Lua supports that

No like

``````local players = game.Players:GetPlayers()
local playersInQueue = {}

local maxPlayers = 10

for i = 1, maxPlayers do
local plr = players[math.random(1, #players)]

if table.find(playersInQueue, plr) then
repeat
plr = players[math.random(1, #players)]
until not table.find(playersInQueue, plr)
table.insert(playersInQueue, plr)
else
table.insert(playersInQueue, plr)
end
end
``````

You can just do this:

``````repeat
table.find(PlayersInQueue, math.random(1, #PlayersInQueue))
until #PlayersInQueue == 20
``````
``````local t = {} --Array of values.
for _ = 1, 10 do
if #t == 0 then break end --Break loop if array reaches 0 values.
local v = table.remove(t, math.random(#t))
--Do something with the pseudo-randomly removed value.
end
``````

Do you care about duplication (is it okay if you get the same value twice)? Can you modify the existing array?