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

How do I make a pair of math.random() generated numbers not repeat itself?

I have a very successful mini-game script part that I've worked on which is:

function InsertColor()
	for i, v in pairs (game.Players:GetPlayers()) do
	local PlayerColor = Instance.new("Color3Value")
	PlayerColor.Parent = v.Character
	PlayerColor.Name = "Color"
	PlayerColor.Value = Color3.new(math.random(), math.random(), math.random())
	TweenColorScreen()
	end
end

Each player would be given a random color: eg: 200, 8, 15 which would be a red dominant color

Out of the thousands of permutations possible, how do I make someone in one game not have the same color as someone else so that there could be only one winner?

Thank You.

1 answer

✓ Accepted answer

First off I would like to say it is important to include the following at the top of your script:

math.randomseed(os.time());

This makes the random numbers even more random.

Then add this: (sorry for weird formatting, I am typing this in web browser and not in an editor)

So basically what you wanna do is store all the random colors in a table, and when you generate a color, you want to check and see if it is already in use.

local colors = {};

function IsInTable(Table,Item)
	for i = 1, #Table, 1 do
 		if (Table[i] == Item) then
			return true;
		end
	end
	return false;
end

function GenrateRandomColor()
	local color = Color3.new(math.random(),math.random(),math.random());
	while true do
		if (IsInTable(colors,color) then
			-- Another player already has this color
			color = Color3.new(math.random(),math.random(),math.random());
		else
			return color;
                end
    	end
end

function InsertColor()
    for i, v in pairs (game.Players:GetPlayers()) do
    local PlayerColor = Instance.new("Color3Value")
    PlayerColor.Parent = v.Character
    PlayerColor.Name = "Color"
    local RandomColor = GenrateRandomColor();
    PlayerColor.Value = Color3.new(RandomColor.r, RandomColor.g, RandomColor.b)
    TweenColorScreen()
    end
end

EDIT: I left out a wait(); at line 14 because it is unlikely you will need to iterate more than once to find a unique random color.

So recap:

- Use math.randomseed(os.time()) to ensure your numbers are actually random

- In the extremely unlikely case that a player will get the same three multi digit / decimal random numbers as someone else, keep track of all the colors and check whether or not it has been used yet, if so generate a new one and repeat.

4 comments

0

If the color is going to be used on bricks, some extra work will be needed to disambiguate between colors, because Color3 values are collapsed to some BrickColor approximations - i.e. two Color3's which are different could lead to the same BrickColor.

1

You forgot to add a 'wait' yield inside your iteration @Line 14

0

@sunwatt Good catch, but I left it out because it is highly unlikely that you will need to iterate more than once to generate a new random number

0

If you want your colors to be bright: local color = Color3.fromHSV(math.random(), 1, 1)

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