Most efficient way to generate a random color?

After doing a bunch of tests I have found out that this is the “best” way to generate a random RGB color, but I still think that there might be a better way, if so could someone tell me what it would be?

local function rc()
	local colorhex = math.random(16^6)-1
	local b=bit32.extract(colorhex,0,8)
	local g=bit32.extract(colorhex,8,8)
	local r=bit32.extract(colorhex,16,8)
	
	return Color3.fromRGB(r,g,b)
end

Sorry if wrong topic or something.

local color = Color3.fromRGB(math.random(1,255),math.random(1,255),math.random(1,255))

This is my way to get random color everytime!

2 Likes

I used to do that, but I’m using my new method since its a bit faster. I’m also guessing that there is no better way to generate a color than what I have right now unless a lua wizard or smth shows up.

local t1 = os.clock()

for i = 1, 100000, 1 do
	local colorhex = math.random(16^6)-1
	local b=bit32.extract(colorhex,0,8)
	local g=bit32.extract(colorhex,8,8)
	local r=bit32.extract(colorhex,16,8)
	
	local color = Color3.fromRGB(r,g,b)
	
end

print(os.clock() - t1)

local t2 = os.clock()
	for i = 1, 100000, 1 do
		local color = Color3.fromRGB(math.random(1,255),math.random(1,255),math.random(1,255))
	end
print(os.clock() - t2)

image

Would you rather have speed, or readability for something as simple as this? I prefer the readability.

-- For brickcolor:
BrickColor.random()
-- For color:
Color3.new(math.random(),math.random(),math.random())
1 Like

Thats why it is in a function and its not that unreadable, but I do get why people would prefer other methods over this since the speed difference is not that big.

1 Like

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