I writing specific admin commands for my game, one of these commands needs to randomise teams. This is the best I could come up with

function randomTeams(target)
	local red = 0
	local blue = 0
	for i, v in pairs(target) do
		local f = math.random(1,2)
		if f == 1 then game.Players[v].Team = game.Teams.Red
		elseif f == 2 then game.Players[v].Team = game.Teams.Blue
		end
	end
	for i, v in pairs(target) do
		if game.Players[v].Team == game.Teams.Red then
			red += red
		elseif game.Players[v].Team == game.Teams.Blue then
			blue += blue
		end
	end
	local difference = red - blue
	while difference > 1 do
		for i, v in pairs(target) do
			if game.Players[v].Team == game.Teams.Red then
				game.Players[v].Team = game.Teams.Blue
				difference -= difference
			end
		end	
	end
	while difference < -1 do	
		for i, v in pairs(target) do
			if game.Players[v].Team == game.Teams.Red then
				game.Players[v].Team = game.Teams.Blue
				difference += difference

			end	
		end
	end
end

The issue is the script never checks the difference between the # of players in each teams to make an adjustment. Would someone either point me in the direction of an area I can learn this better, or point out the error in the script?