Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

I used math.random but my code isn't random?

Asked by 7 years ago
Edited 7 years ago

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()
0
just test game in a server and not in studio CootKitty 311 — 7y
0
read my edit hiimgoodpack 2009 — 7y

4 answers

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

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!

0
After doing this the color of the brick didn't change at all. funface101 45 — 7y
0
Did you not handle all the other numbers it could return? hiimgoodpack 2009 — 7y
0
Well, this script mainly accounts for how many bricks the script changes. So I expect that with over 50 bricks it will change at least one. But they all stayed the same. funface101 45 — 7y
0
Because all those scripts execute at the same time! hiimgoodpack 2009 — 7y
View all comments (2 more)
0
So is there anyway to make it random with them executing at the same time? funface101 45 — 7y
0
okay ill edit my script hiimgoodpack 2009 — 7y
Ad
Log in to vote
1
Answered by 7 years ago

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:

  • Using 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
  • You call :Clone() on line 7 but don't do anything with the result (which just destroys the Clone), though you probably don't need the line anyway as you already cloned something on line 6
  • Note that you're supposed to assign the parent last (as it is significantly more efficient to assign the CFrame first, then set the parent). In this case, that means right before you destroy script.Parent.
Log in to vote
0
Answered by 7 years ago

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).

http://wiki.roblox.com/index.php?title=Random_numbers#Generating_Different_Random_Numbers_Every_Time_the_Game_Runs

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.

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

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

0
Everyone has been telling me to do this, but all these scripts execute at the same time so they still get the same or around the same return. funface101 45 — 7y
0
ok let me test out your script and i will find a good solution Subaqueously 11 — 7y
0
Thanks, I have been confused by this, and my game progress will continue after this, as this is a big part of the game. funface101 45 — 7y
0
ok can you tell me what you're trying to do and post more code so I can fully understand the issue Subaqueously 11 — 7y
View all comments (4 more)
0
also, i see no point in "more random" Subaqueously 11 — 7y
0
I just edited the main code with the full code. funface101 45 — 7y
0
ok im going to edit my answer copy paste see if it works Subaqueously 11 — 7y
0
have you not read my edit @Subaqueously? hiimgoodpack 2009 — 7y

Answer this question