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

how to get 4 random values from a table?

Asked by
theCJarmy7 1293 Moderation Voter
8 years ago
vals = {
    [1] = "1shfh",
    [2] = "2shfh",
    [3] = "3shfh",
    [4] = "4shfh",
    [5] = "5shfh",
    [6] = "6shfh",
    [7] = "7shfh",
    [8] = "8shfh",
    [9] = "9shfh",
    [10] = "10shfh" -- this is just a test table, if you were wondering
}


maps1 = math.random(1,#vals)
table.remove(vals, maps1)
maps2 = math.random(1,#vals)
table.remove(vals, maps2)
maps3 = math.random(1,#vals)
table.remove(vals, maps3)
maps4 = math.random(1,#vals)
table.remove(vals, maps4)
while wait() do
print(maps1,maps2,maps3,maps4)
end

it ALWAYS prints 9 9 2 3. its supposed to give me 4 different numbers, and i cant have them repeat, thats why there is the table.remove

2 answers

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
8 years ago

If you're not getting different numbers everytime, try using math.randomseed!

local vals = {1,2,3,4,5,6,7,8,9,10}; --Your table
local maps = {};

for i = 1,4 do
    table.insert(maps,table.remove(vals,math.random(1,#vals)));
end

print(table.concat(maps,", "));
0
I did the same thing you did, but rather than the repetitive usage of accessing a random index, then removing it from the table. I just used a for loop and put them all into the 'maps' table. Goulstem 8144 — 8y
0
uh, where in the world did you get maps from? it was just a name. also, this gives me all the values. i am so confused. theCJarmy7 1293 — 8y
0
No it doesn't, it gives you 4 values. And I made 'maps' as a holder for the random maps that were extracted from 'vals' Goulstem 8144 — 8y
0
somethings weird. its not random. it ALWAYS print: 9, 10, 2, 4 theCJarmy7 1293 — 8y
View all comments (4 more)
0
It's always random for me when I put it into the command bar :P Goulstem 8144 — 8y
0
really? shame i cant use the command bar for this. theCJarmy7 1293 — 8y
0
LOLOLOL koolkid8099 705 — 8y
0
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ theCJarmy7 1293 — 8y
Ad
Log in to vote
1
Answered by 8 years ago

Overview:

You seem to be going about this correctly, though it could be a bit cleaner. But we'll get to that later.

Problem:

The problem is pretty simple. You're creating 4 individual variables, each assigned a random value from your array (btw, assigning the numeric keys manually isn't necessary).

The problem is, the values that were assigned to each of those variables you created, stay stored in those variables. They won't change unless you manually do it, or have a function do it.

Solution:

We can fix this easily by putting our code inside the while loop, to refresh each operation. Here's a function i devised that grabs a random value from an array, implemented in your revised code:

local vals = {
    "first",
    "second",
    "third",
    "fourth",
    "sixth",
    "seventh",
    "eighth",
    "ninth",
    "tenth",
}

local function randomValue(t)
    return t[math.random(#t)]
end

-- Slow it down to make it a bit easier to see the changes
while wait(1) do
    local randoms = {}

    -- Create the random variable 4 times within the "randoms" table
    for i = 1,4 do
        randoms["map"..i] = randomValue(vals)
    end

    -- Print all elements in the "randoms" table.
    print(unpack(randoms))
end

Hope it helped. Let me know if you have any questions.

0
didnt this have an accepted answer when you posted this answer? this was really detailed, and if goulstem hadnt answered it, this would be the answer. theCJarmy7 1293 — 8y

Answer this question