Math.random same across five scripts

i have five scripts that runs this:

local skin = {Color3.fromRGB(255, 204, 153),Color3.fromRGB(90, 76, 66),Color3.fromRGB(143, 76, 42),Color3.fromRGB(204, 142, 105),Color3.fromRGB(160, 95, 53),Color3.fromRGB(234, 184, 146),Color3.fromRGB(124, 92, 70),Color3.fromRGB(108, 88, 75),Color3.fromRGB(170, 85, 0)}
local skincolor = skin[math.random(1, #skin)] 

however when the game starts, it loads up the same color on every NPC
i think the random seed generator might use os.time, which would provide randomness, but I need the five npc have different seeds or use another way, any ideas?

2 Likes

Make sure it is a local script

does that affect the random number generation? I kinda need this inside the NPC

Actually ignore that comment its late but i think the problem is to make something like this: Color3.fromRGB(Math.random(0, 255), Math.random(0, 255), Math.random(0, 255))

the script is drawing a pool of selected skin colors, the math.random is on the second line

I dont know if math.random would work there but you can try mine also if its drawing a pool of selected skin colors then why do you need the first line?

:skull: i don’t think you understand
the first like is the pool of skin colors, this script does work. But the problem is it generates the same color when the game first starts, which is not good

Oh i recomend doing it on a main script and just getting it from there,

do you even understand my problem :grinning:

You can set the seed manually by using math.randomseed(x).
I’m not sure how random you need it but if you use a different value in each script plus os.time, should work.

It’s because the seed is determined by the tick, so when you run the script, it gets the same seed because they run at the same exact time.

The Random object does not seem to be affected though, so I think it might work too. To use it, just create one using Random.new() and call NextInteger or NextNumber on it.

good point but it is exactly what i am avoiding, setting the seed manually/using tick/os.time means if the five scripts run at the same time, same colors

yeah i figured it had something to do with time, but what can i use the random.new on? since tick isn’t a option anymore

I don’t really know what you mean by that. What do you mean by tick isn’t an option anymore? You can still set the seed to it.

as you said, since the scripts all run on the same tick, wouldn’t it have same results?

oddly brickcolor.random is truly random, i wonder what it uses differently

You could use a bindable function to get an incremented value to set the seed.
Something like:

--npc script
local ranSeed = bindFunc:Invoke()
math.randomSeed(ranSeed)
local skin = {Color3.fromRGB(255, 204, 153),Color3.fromRGB(90, 76, 66),Color3.fromRGB(143, 76, 42),Color3.fromRGB(204, 142, 105),Color3.fromRGB(160, 95, 53),Color3.fromRGB(234, 184, 146),Color3.fromRGB(124, 92, 70),Color3.fromRGB(108, 88, 75),Color3.fromRGB(170, 85, 0)}
local skincolor = skin[math.random(1, #skin)]

--othr script
local Inc = 1
bindFunc.OnInvoke = function()
    Inc *= 2
    return Inc + os.time()
end

Not sure why I wrote it that way, this could be more straightforward:

--npc script
local skin = {Color3.fromRGB(255, 204, 153),Color3.fromRGB(90, 76, 66),Color3.fromRGB(143, 76, 42),Color3.fromRGB(204, 142, 105),Color3.fromRGB(160, 95, 53),Color3.fromRGB(234, 184, 146),Color3.fromRGB(124, 92, 70),Color3.fromRGB(108, 88, 75),Color3.fromRGB(170, 85, 0)}
local ranNum = bindFunc:Invoke(1, #skin)
local skincolor = skin[ranNum]

--othr script
bindFunc.OnInvoke = function(low, high)
    return math.random(low, high)
end

From my understanding this should work because when the seed is set at the start of the game it creates a sequence of pseudo random numbers, calls to math.random access the next in the sequence and as calls to this are now in the same scope they should access it sequentially despite the tick being the same. Whereas, previously each thread was accessing the first in the sequence because the seeds were identical in each thread.

I don’t think so. Random.new() seems to be different from math.random. I just checked.

1 Like