 # Need help with LUA random loop

Hello FP,

I’m using this example from the gmod wiki:

===
websites = { “facepunch.com”, “google.com”, “steampowered.com” }

# print( "I think the best website ever is " … websites[math.random( 1, #websites )] … “.” )

When the script is called it prints out a random website from the array above into console,

How would I loop through the array in a random order but make it so each time the script is called it doesn’t repeat the websites before it?

e.g.
numbers = { “1”, “2”, “3” }

print(numbers[math.random( 1, #numbers )])

I would like it to pick a random number from the array above and loop through the values randomly and independently until all the values have been used.

So calling the script could return a 2, then a 1, then a 3 and then 2 and then 1 and then 3 so it would only be a duplicate if all the numbers have been called before. I do not want the script to print out all the values at once, just a random one each time without the future numbers being a duplicate until all the numbers have been used before the script is called.

Apologies for the confusing explanation.

Thanks.

I see you like loops, so I made a recursive loop for you to loop.

[lua]
numbers = {
“1”,
“2”,
“3”
}

function RandomOutput(tab)

``````if #tab &lt; 1 then return end
local pick = math.random(1, #tab)
print(tab[pick])
table.remove(tab, pick)
RandomOutput(tab)
``````

end

RandomOutput(numbers)
[/lua]

In regards to your question, assuming that you’re running the script manually:

[lua]
numbers = numbers or { 1, 2, 3 }

local rn = math.random( #numbers )
print( numbers[ rn ] )
table.remove( numbers, rn )
if #numbers == 0 then numbers = nil end
[/lua]

However, it’s a little unclear what the use case for this is; this will only work correctly if the script file itself is run multiple times, and also has the problem of polluting the global namespace.

It may help if you explain your intended purpose?

Thanks Blasteh but I only wanted to print one value at a time, Lixquids code was perfect. Thanks.

You can shuffle the table or randomly pick positions and remove them.

What would be the procedure to never repeat the last value given? So it can constantly be run without repeats as you can have 1 then 2 then 3 then 3 again. I do not want all the values to be looped through at one time, only individually but never the same as the last value and can be run multiple times without the end value of the first loop and start value of the new loop being the same.

Thanks.

You could define a separate local variable and set it to the index of the last pick. Then, you can check to see if the random number is equal to the variable. If it is, then you can edit the value (make it the random number plus or minus 1) so that the value is not equal to the last one or simply return. Since you want it to continue constantly, just use a timer. If you use Blasteh’s recursive function combined with not removing values, you’d cause an infinite loop.

``````

local numbers = {1, 2, 3}
local lastvalue = 0
timer.Create('RandomLoop', 1, 0, function()
if #numbers == 0 then return end
local pick = math.random(1, #numbers)
if pick == lastvalue then return end
lastvalue = pick
print(tostring(numbers[pick]))
--Do shit with the value here.
end)

``````