lua随机值

产生3以下的数字

local count = 3
math.randomseed(os.time())
local index = math.random(count)
ngx.say(index);

产生 10~13 的数字

math.randomseed(os.time())
local index = math.random(10,13)
ngx.say(index);
local res = ngx.location.capture_multi("http://baidu.com", {method = ngx.HTTP_GET,args = {a = 1, b = '2&'},body = 'c=3&d=4%26'})
ngx.say(res.body)

取出随机值

function readRandomValueInTable(Table)
    math.randomseed(os.time())
    return Table[math.random(1,#Table)]
end

local tables = {'192.168.1.1','172.17.1.1','10.11.9.2'}
local postion = readRandomValueInTable(tables)
ngx.say(postion);

如果是哈希表要全部按key全部的话

Last updated