• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua math.random bug?

silveralol

Advanced OT User
Joined
Mar 16, 2010
Messages
1,483
Solutions
9
Reaction score
215
well, I have some problems with a script to load maps of the world change fury gate...
my script
Code:
local config = {
    [1] = { -- Ab'dendriel
        storage = 1,
        fromPosition = Position(32673, 31716, 7),
        toPosition = Position(32687, 31728, 7),
        mapName = 'ab',
    },
    [2] = { -- Ankrahmun
        storage = 2,
        fromPosition = Position(33257, 32834, 7),
        toPosition = Position(33272, 32846, 7),
        mapName = 'ankrahmun',
    },
    [3] = { -- Carlin
        storage = 3,
        fromPosition = Position(32258, 31842, 7),
        toPosition = Position(32266, 31852, 7),
        mapName = 'carlin',
    },
    [4] = { -- Darashia
        storage = 4,
        fromPosition = Position(33292, 32365, 7),
        toPosition = Position(33307, 32377, 7),
        mapName = 'darashia',
    },
    [5] = { -- Edron
        storage = 5,
        fromPosition = Position(33214, 31912, 7),
        toPosition = Position(33227, 31928, 7),
        mapName = 'edron',
    },
    [6] = { -- Kazordoon
        storage = 6,
        fromPosition = Position(32572, 31976, 7),
        toPosition = Position(32578, 31983, 7),
        mapName = 'kazordoon',
    },
    [7] = { -- Liberty Bay
        storage = 7,
        fromPosition = Position(32344, 32689, 7),
        toPosition = Position(32357, 32702, 7),
        mapName = 'liberty',
    },
    [8] = { -- Port Hope
        storage = 8,
        fromPosition = Position(32526, 32708, 7),
        toPosition = Position(32537, 32720, 7),
        mapName = 'port',
    },
    [8] = { -- Thais
        storage = 9,
        fromPosition = Position(32260, 32157, 7),
        toPosition = Position(32275, 32171, 7),
        mapName = 'thais',
    },
    [10] = { -- Venore
        storage = 10,
        fromPosition = Position(32828, 32080, 7),
        toPosition = Position(32836, 32087, 7),
        mapName = 'venore',
    }
}
local FuryEnabled = true
function onStartup()
    if FuryEnabled then
        local randTown = config[math.random(#config)]
        if not randTown then
            return true
        end
        Game.loadMap('data/world/furygate/' .. randTown.mapName .. '.otbm')
        print(">> The fury gate are in " .. randTown.mapName .." today.")
        Game.setStorageValue(GlobalStorage.FuryGates, randTown.storage)
        return true
    end
end
this always will be in edron ... I don't know why ...
I have another script that is the same thing, the math.random always stay in one "number"
Code:
local TeleportToJaul = Position(33545, 31263, 11)
local TeleportToObujos = Position(33419, 31255, 11)
local TeleportToTanjis = Position(33639, 31230, 11)
function onStartup()
    local rand = math.random(1, 100)
    if rand < 20 then
        local teleportJaul = Game.createItem(1387, 1, Position(33558, 31282, 11))
        if teleportJaul:isTeleport() then
            teleportJaul:setDestination(TeleportToJaul)
            Game.createMonster("Jaul", Position(33538, 31269, 11))
            print(">> Deeplings World Change reveals Jaul today.")
        end
    elseif rand >= 20 and rand < 40 then
        local teleportObujos = Game.createItem(1387, 1, Position(33438, 31248, 11))
        if teleportObujos:isTeleport() then
            teleportObujos:setDestination(TeleportToObujos)
            Game.createMonster("Obujos", Position(33434, 31262, 11))
            print(">> Deeplings World Change reveals Obujos today.")
        end
    elseif rand >= 40 then
        local teleportTanjis = Game.createItem(1387, 1, Position(33649, 31261, 11))
        if teleportTanjis:isTeleport() then
            teleportTanjis:setDestination(TeleportToTanjis)
            Game.createMonster("Tanjis", Position(33646, 31243, 11))
            print(">> Deeplings World Change reveals Tanjis today.")
        end
    end
    return true
end
the second script is always in the number > 20 < 40...
 
add line
local randTown = math.randomseed(os.time())
before
local randTown = config[math.random(#config)]

and
local rand = math.randomseed(os.time())
before
local rand = math.random(1, 100)
 
add line
local randTown = math.randomseed(os.time())
before
local randTown = config[math.random(#config)]

and
local rand = math.randomseed(os.time())
before
local rand = math.random(1, 100)
I can ask why declare two times the same variant ?
I use it, is correct?
Code:
local randTown = math.randomseed(os.time())
randTown = config[math.random(#config)]

local rand = math.randomseed(os.time())
rand = math.random(1, 100)
works fine... but I don't know if is the correct way to use it, anyway, thank you for it :3
 
TFS 1.2 already seeds math.random on startup, I believe you have removed this line from your startup.lua.

Code:
math.randomseed(os.mtime())

It is only necessary to call this once.
 
Back
Top