Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

how to get 4 random values from a table?

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

✓ Accepted answer

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,", "));

8 comments

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.

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.

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'

0

somethings weird. its not random. it ALWAYS print: 9, 10, 2, 4

0

It's always random for me when I put it into the command bar :P

0

really? shame i cant use the command bar for this.

0

LOLOLOL

0

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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.

1 comment

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.