• 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

soul4soul

Intermediate OT User
Joined
Aug 13, 2007
Messages
1,875
Solutions
3
Reaction score
128
Location
USA
i use this code right now
Code:
eq = math.random(1,6)
if eq == 1 then
                    doPlayerAddItem(cid, 9927, 1)
                elseif eq == 2 then
                    doPlayerAddItem(cid, 9929, 1)
                elseif eq == 3 then
                    doPlayerAddItem(cid, 9928, 1)
                elseif eq == 4 then
                    doPlayerAddItem(cid, 9931, 1)
                elseif eq == 5 then
                    doPlayerAddItem(cid, 2527, 1)
                elseif eq == 6 then
                    doPlayerAddItem(cid, 7697, 1)
                end

if there any way to simplify it? would something like this work
Code:
doPlayerAddItem(cid, math.random({9927,9929,9928,9931,2527,7697}), 1)
 
Lua:
local t = {
    [1] = 9927,
    [2] = 9929,
    [3] = 9928,
    [4] = 9931,
    [5] = 2527,
    [6] = 7697,
}
               
doPlayerAddItem(cid, t[math.random(6)], 1)
 
have you seen mock's function? :p
Lua:
function choose(...) -- by mock
    local arg = {...}
    return arg[math.random(1,#arg)]
end
 
doPlayerAddItem(cid, choose(9927,9929,9928,9931,2527,7697), 1)
 
oh!! i actually have. i can believe i forgot about it. even better b/c i have a few pieces of code that needed this.
 
cyberM here you forget a [1] look
Lua:
local t = {
    [1] = 9927,
    [2] = 9929,
    [3] = 9928,
    [4] = 9931,
    [5] = 2527,
    [6] = 7697,
}
 
doPlayerAddItem(cid, t[math.random(6)][1], 1)
 
nope, he forget 1 in math.random, cause now it may return 0 :p, and 1 comma too much.
Lua:
local t = {
    [1] = 9927,
    [2] = 9929,
    [3] = 9928,
    [4] = 9931,
    [5] = 2527,
    [6] = 7697
}
 
doPlayerAddItem(cid, t[math.random(1, 6)], 1)
 
cyberM here you forget a [1] look
Lua:
local t = {
    [1] = 9927,
    [2] = 9929,
    [3] = 9928,
    [4] = 9931,
    [5] = 2527,
    [6] = 7697,
}
 
doPlayerAddItem(cid, t[math.random(6)][1], 1)
it's the same thing :p t[math.random(6)] = t[math.random(6)][1]
there's only 1 value
nope, he forget 1 in math.random, cause now it may return 0 :p, and 1 comma too much.
Lua:
local t = {
    [1] = 9927,
    [2] = 9929,
    [3] = 9928,
    [4] = 9931,
    [5] = 2527,
    [6] = 7697
}
 
doPlayerAddItem(cid, t[math.random(1, 6)], 1)
not really :p read lua math.random manuals
math.random(6) = math.random(1, 6)
 
Back
Top