Here's the problem. I did this code and even added the extra randomness (As you can tell by the more random varible), but I had it change the block to a different color, but every single time it gave me the same color. How can I get it to actually be random? Note: If I add math.randomseed(tick()) it will not do anything since the scripts execute at the same time.
stoneblocksetup = game.ReplicatedStorage["Teir #1"]["Rock #0003"] local morerandom = math.random(15, 30) local stone = math.random(3,morerandom) print(stone) if stone == 17 or stone == 5 then stoneblock = stoneblocksetup:Clone() stoneblock:Clone() stoneblock.Parent = script.Parent.Parent stoneblock.Name = "Rock" stoneblock.CFrame = script.Parent.CFrame script.Parent:Destroy() end script:Destroy()
This is because math.random is NOT ACTUALLY RANDOM. It is based off it's seed, but I don't know what it does with the seed. To make this different every time, use
math.randomseed(tick() * wait() / wait()) local morerandom = math.random(15, 30) local stone = math.random(1,morerandom) if stone == 25 then (Code here) end
Hope this helps!
First, you need math.randomseed(tick()) math.random()
in one script that runs at the start of your place (and is never run a 2nd time). If you initialize a randomseed more than once, you'll reset how the random numbers are generated. ex, if you run the following script multiple times, it will always print out the same 3 values:
math.randomseed(1) print(math.random()) print(math.random()) print(math.random()) --Output: 0.56358531449324 0.19330423902097 0.80874050111393
So if each of your scripts tries to call math.randomseed and then you ask for a random number (or several random numbers), they will always turn out to be the same.
But if you do this script:
math.randomseed(tick()) print(math.random()) print(math.random()) print(math.random())
It will print out different values each run (so long as you start each run during a different second -- otherwise tick()
will return the same thing). However, you might notice that the first value is similar each time. This is why you should always "throw away" the first random value after using randomseed. and is why the math.random()
can go right after the call to randomseed.
As for your script, some notes:
morerandom
doesn't really do anything. If you want there to be a 10% chance of something to happen, for instance, you should just do if math.random(1, 10) == 1 then
or if math.random() <= 0.1 then
There are some official strategies for getting that extra-randomness you are looking for. The use of two variables is creative, but I'll leave it to the math people to explain why it's not very random actually (I don't know the specific reasons).
To receive a different value every time the function is called, you need to set the seed to a different value when the game runs
math.randomseed(tick())
That at the beginning of your script should make it more random.
I've always had this problem, but it's very easy to fix. Just add math.randomseed(tick()) at the first line of your script to change its seed. This means that the seed that generates the "randomness" changes to "make it more random / actually random".
math.randomseed(os.time()) local morerandom = math.random(15, 30) local stone = math.random(3, morerandom) print(stone) if stone == 17 or stone == 5 then stoneblock = stoneblocksetup:Clone() stoneblock:Clone() stoneblock.Parent = script.Parent.Parent stoneblock.Name = "Rock" stoneblock.CFrame = script.Parent.CFrame script.Parent:Destroy() end script:Destroy()
BTW i tried it with math.randomseed(tick()) and it worked fine but try this instead