I’m trying to return a random BrickColor for the user’s color of magic, but the colors arent so random as players tend to spawn in with the same colors.
I tried math.randomseed(tick()) and it wasn’t so random, so I then tried this
function returnRandomColor()
math.randomseed((tonumber(os.time())+tonumber(tick())-(tonumber(tick())/2)))
return BrickColor.new(math.random(), math.random(), math.random())
end
Although, that returns the same BrickColor a lot too. Is there any way I can get this more randomized?
I made a pretty verbose implementation and tested it just now in Studio. Every time it’s executed it’s random.
-- Ensure every time the script is executed, create a new seed for Lua's
-- pseudo-random generator. We use os.time() since it will be different every
-- time the script is executed (given that it's executed in intervals greater
-- than one second. This is random enough).
math.randomseed(os.time())
--- Returns a random BrickColor.
--- @return BrickColor
local function getRandomBrickColor()
local randomR = math.random();
local randomG = math.random();
local randomB = math.random();
local randomBrickColor = BrickColor.new(randomR, randomG, randomB);
return randomBrickColor;
end
Only ever set the seed once, and it doesn’t need to be elaborate.
For similar seeds, the first number or two are always the same. So setting the seed and generating a number, then setting the seed and generating another number will yield the same number with a raw tick. If you set the seed only once, at the top of the script, it will set the seed and all random numbers after that will follow in a random fashion.
BrickColor.new() just constructs the closest BrickColor. For specific colors, you should use Color3.
I read the API this time (lol) and found that it does not have a random constructor. Opting to keep it simple, you could create a randomize function for Color3 like so through the Color3.new factory:
Here is a verbose implementation:
math.randomseed(os.time())
--- Creates a random Color3.
--- @return Color3
local function createRandomColor3()
local randomR = math.random();
local randomG = math.random();
local randomB = math.random();
local randomColor3 = Color3.new(randomR, randomG, randomB);
return randomColor3;
end
Another way for using RGB cause Bricks accept Color and BrickColor:
local function RandomizeColor3()
local r, g, b = math.random(0, 255), math.random(0, 255), math.random(0, 255);
return Color3.fromRGB(r, g, b);
end);
-- Saying that the script is in the part
script.Parent.Color = RandomizeColor3();
Edit: @TheEdgyDev thanks for pointing out my mistake with assigning multiple variables one value.
Syntax errors aside, this code semantically does not work as intended.
g and b are always equal to nil as they are never assigned, meaning that the colors will be either black or some form of red. Here is your code adapted to work:
local function RandomizeColor3()
local r, g, b = math.random(0, 255), math.random(0, 255), math.random(0, 255);
return Color3.fromRGB(r, g, b);
end
script.Parent.Color = RandomizeColor3();
Oh whoops, may of gotten used to how some functions can pass two or more variables. Forgot math.random() doesn’t replicate on multiple singular variables.
If you wanted a full rainbow Color3 instead, than this should work (works with multiple objs. ).
local objs = {--[[parts, otherstuff ]]}
while true do
for i=0, 1, 1 / 300 do
for _, obj in next, objs do
obj.Color = Color3.fromHSV(i, 1, 1);
end
wait()
end;
end