How to select multiple random numbers

I am trying to make a random map selector and I need to select 3 maps. I have everything set up, but I need to get 3 random numbers that are different from each other. I thought of looping until you get a unique number, but there is a rare situation where your game can crash by selecting the same number a lot of times. I’ve been scratching my head for a bit on this. Thanks!

Make a table, pick a map at random, remove it from the table, and repeat this process two more times.

For anyone who needs it in the future, here is some sample code that could work for this scenario:

-- Tables --

local Maps = {
	"Dust",
	"Grasslands",
	"Desert",
	"Factory",
	"Baseplate"
}

-- Functions --

function SelectRandomMaps(Count)
	local TempMapCopy = table.clone(Maps)
	local SelectedMaps = {}
	
	for i = 1, Count, 1 do
		local RandomIndex = math.random(1, #TempMapCopy)
		table.insert(SelectedMaps, TempMapCopy[RandomIndex])
		table.remove(TempMapCopy, RandomIndex)
	end
	
	return SelectedMaps
end


-- Scripting --

print(SelectRandomMaps(3))
3 Likes

lol. don’t know how i didnt think of that

1 Like

Sorry, but how do you know that the number will be unique?

No worries. The key is not that I am generating a unique number, but the fact that I am removing anything already picked as an option. As you can see, I am cloning the table, and for every iteration of the for loop (so for every new map), I am picking a map from the table and then removing that map from the table.

Say we have the following tables when we start:

local TempMapCopy = {
	"Dust",
	"Grasslands",
	"Desert",
	"Factory",
	"Baseplate"
}

local SelectedMaps = {}

When I pick a random number [local RandomIndex = math.random(1, #TempMapCopy)], it is picking a number between 1 - 5. After the number has been picked, I will access that item in the table and store it in our SelectedMaps table [table.insert(SelectedMaps, TempMapCopy[RandomIndex])]. After that, I will remove the item from the table [table.remove(TempMapCopy, RandomIndex)].

Lets say that our RandomIndex was 4. That means that our “picked” map is “Factory.” I will now add that map to our SelectedMaps and remove it from the TempMapCopy table. Now our tables look like this:

local TempMapCopy = {
	"Dust",
	"Grasslands",
	"Desert",
	"Baseplate"
}

local SelectedMaps = {
    "Factory"
}

See how it doesn’t exist anymore in the TempMapCopy? Well, if we repeat the process, we are now choosing a number between 1 - 4 because there are now 4 items. I’ll pick 2 as our random number now. Here is how our table looks:

local TempMapCopy = {
	"Dust",
	"Desert",
	"Baseplate"
}

local SelectedMaps = {
    "Factory",
	"Grasslands"
}

Now it will choose a random number between 1 - 3. I don’t think I need to continue this further. You should understand that it doesn’t really pick a “unique” number, but rather eliminates whatever has already been chosen from selection.

Hopefully this helped you understand it better.

Thank you so much! I understand and appreciate the explanation and it is working!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.