Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Trying to pick 6 random values each time inside of array?

Asked by 5 years ago

Okay I understand how to pick them, but my table.remove thing isn't working and I know it isn't efficient, So I was wondering how the hell you can check if it is the same as the last variaible. Look at my script here:

local Array = {"Value1","Value2","Value3","Value4","Value5","Value6","Value7","Value8","Value9"}

local Variable1 = Array[math.random(1, #Array)]
local Variable2 = Array[math.random(1, #Array)]
local Variable3 = Array[math.random(1, #Array)]
local Variable4 = Array[math.random(1, #Array)]
local Variable5 = Array[math.random(1, #Array)]
local Variable6 = Array[math.random(1, #Array)]

print(Variable1)
print(Variable2)
print(Variable3)
print(Variable4)
print(Variable5)
print(Variable6)

So it will obviously select a random value each time, but it may not be different from another value chosen, I was wondering how to check without being too tedious?

3 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

Following exactly what theCJarmy7 is doing, if you wish to remove the variable so it doesn't reappear twice, just change the getRandomValue function a little

local function getRandomValue()
    local number = math.random(1, #array)
    local tempvariable = Array[number]
    table.remove(Array, number)
    return tempvariable
end

Ad
Log in to vote
2
Answered by
theCJarmy7 1293 Moderation Voter
5 years ago
Edited 5 years ago

You could use a for loop to get a random value each time.

local array = {"Value1","Value2","Value3","Value4","Value5","Value6","Value7","Value8","Value9"}

local function getRandomValue()
    return array[math.random(1,#array)]
end

--This will be used to check if we already got something from the table
local randomVals = {}

for q=1,6 do
    local val
    repeat
        val = getRandomValue()
    until not randomVals[val]

    randomVals[val] = true
end

for i,v in pairs(randomVals) do
    print(i,v) --prints 6 different vals.
end

You could also do something with table.remove if you wanted to.

0
This is inefficient, but I see how this works so this is one way to do it MusicalDisplay 173 — 5y
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Here is a generic solution to this problem, should work for any size array and supports checking for duplicates also. Uses the new Random API also so it is not biased.

--[[
    Array = Table to get values from
    Amount = Amount of values to get
    Different = Check if value is already in return
--]]
local function GetRandomValues(Array, Amount, Different)
    if (#Array < Amount and Different) then error("Amount specified is more then array contains.") end
    local Rnd = Random.new()
    local Ret = {}
    if Different then
        local AlreadyUsed = {}
        for I=1,Amount do
            local Found = Rnd:NextInteger(1, #Array)
            while AlreadyUsed[Found] ~= nil do Found = Rnd:NextInteger(1, #Array) end
            table.insert(Ret, Array[Found])
            AlreadyUsed[Found] = true
        end
    else
        for I=1,Amount do
            table.insert(Ret, Array[Rnd:NextInteger(1, #Array)])
        end
    end
    return Ret
end

--Test call
print(unpack(GetRandomValues({"asd", "asd2", "asd3", "asd4", "asd5", "asd6"}, 3, false))) --asd5 asd2 asd2
print(unpack(GetRandomValues({"asd", "asd2", "asd3", "asd4", "asd5", "asd6"}, 3, true))) --asd5 asd3 asd

Answer this question