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

Why does this script print the same thing every time?

Asked by
NRCme 0
8 years ago

I had it working before it crashed but now I need to try and fix it...

here is my code:

    local rndm1 = math.random()
    local rndm2 = math.random()
    local rndm3 = math.random()
    local rndm4 = math.random()
    local UIP = workspace.UIP.Value

    while true do
        if rndm1 == 0 then
            rndm1 = math.random()
        else
            break
        end
        wait()
    end
    while true do
        if rndm4 == 0 then
            rndm4 = math.random()
        else
            break
        end
        wait()
    end

    local rndm11 = math.ceil(rndm1*254)
    local rndm22 = math.ceil(rndm2*254)
    local rndm33 = math.ceil(rndm3*254)
    local rndm44 = math.ceil(rndm4*254)

    local StaticLocalIP = rndm11.. ".".. rndm22.. ".".. rndm33.. ".".. rndm44
    print(StaticLocalIP)

when I tried putting everything in a while loop it worked but when I took it out I just kept getting this number in the output:

100.33.234.106

Any ideas on why this is happening?

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
8 years ago

Problems

Okay, first of all your while loops on line 7 and line 15 are redundant, since neither 'rndm' variable will ever be equivalent to 0.

math.random(), given no parameters or arguments, generates a random number between 0 and 1. Since it's between 0 and 1, it is not inclusive of 0 or 1. 0 and 1 cannot be chosen, so you might as well get rid of both those loops all together.


Now, secondly.. the reason why you're ending up with the same result everytime is because you are using math.random with the same seed everytime. When you press play, unless otherwise specified, the randomseed will always be 1. This makes it so the first random decision with the same arguments in a server will be the same everytime.

What you have to do is just change the seed, relevant to your current time. This can be accomplished using the tick function.

math.randomseed(tick())

If you add this to the top line of your code, everytime the server starts the randomseed will be changed relevant to your current time rather than always being 1. math.random is generated off of the set randomseed, so by changing the seed you change the results :)



Code

math.randomseed(tick())

local rndm1 = math.random()
local rndm2 = math.random()
local rndm3 = math.random()
local rndm4 = math.random()
local UIP = workspace.UIP.Value

local rndm11 = math.ceil(rndm1*254)
local rndm22 = math.ceil(rndm2*254)
local rndm33 = math.ceil(rndm3*254)
local rndm44 = math.ceil(rndm4*254)

local StaticLocalIP = rndm11.. ".".. rndm22.. ".".. rndm33.. ".".. rndm44
print(StaticLocalIP)
Ad

Answer this question