Is this a good randomizer?

I’ve seen a few other team-randomizers that are much longer and more complicated so I wrote my own. The purpose of the script below is to create even teams, a 4v4, 8v8, so on and so forth basically. Is it decent or should I change it?

	local players = game.Players:GetPlayers() -- Gets a table of all of the players in the server.
	local AmountOfTeams = ((#game.Teams:GetChildren())-1)
	local playersPerTeam = math.ceil(((#game.Players:GetPlayers())/AmountOfTeams))
	if (playersPerTeam < 1) then playersPerTeam = 1 end -- Only 1 player in-game
	print("Players per team: " ..playersPerTeam)
	print("Amount of Teams: " ..AmountOfTeams)
	print("Player Count: " ..#players)
	
	for i = 1,#players do -- Shuffling the table
		local newpos = math.random(1,#players)
		local curVal = players[newpos]
		players[newpos] = players[i]
		players[i] = curVal
	end

	local iteration = 1
	for _,a in pairs(game.Teams:GetChildren()) do
		print(a.TeamColor)
		if a.TeamColor ~= BrickColor.new("White") then -- We aren't putting players on the lobby team.
			for i = 1,playersPerTeam do
				if not players[iteration] then break end
				players[iteration].TeamColor = a.TeamColor
				iteration = iteration + 1
			end
		end
	end
	print("Completed randomizing teams")
end
3 Likes

Shuffling like this by swapping each element with a random element from anywhere in the table does not actually produce a uniform distribution, see this Wikipedia article.

As shown in that article (altered to Lua’s 1-based indexing), I would shuffle like this:

for i = 1, #players - 1 do
  	--select a player from the not-yet-shuffled part of the list
    	local newpos = math.random(i,#players) --i, not 1
	local curVal = players[newpos]
	players[newpos] = players[i]
	players[i] = curVal
end
10 Likes

Very interesting, I had never thought of it like that before.

If you are aiming for random, get numbers from the players individually. Do this by using a RemoteFunction that fires at a client, and that client returns a random number.

Use this sparingly, using this method too much will drastically increase both the server and client ping.

If you need help with Remote Functions, see this wiki page: https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events

This is more random because each client was run at a different time, meaning every client will have a different number. When the server tries to just to do math.random(), it may or may nor get the same number repeated a couple times. This is because the “random” function in roblox is actually the solver giving a series of numbers that scripts can reference to. math is pretty much a table, and all everything under it is a function.

The math.random function is returning the most recent value it finds from the ACII solver. math.random is not a number; it is a function, returning a number. Keep that in mind.

When a client calls the random function, it is not getting the number from the server ACII solver, but rather from the client runner.

I really hope this makes sense, because knowing this will help with any projects in the future.

You don’t need this random of a random number generator to shuffle a table…
A simple math.random() on the server will suffice.

1 Like