local ro = {} ro.rnds = {} ro.numrnd = 300 ro.trnmin = 1 ro.trnmax = 1000000000 ro.nxtidx = 1 ro.topidx = 0 ro.finit = false ro.init = function(numrnd) if true == ro.finit then return end math.randomseed(os.time()) ro.finit = true if nil == numrnd then numrnd = ro.numrnd end url = "https://www.random.org/integers/?num="..tostring(numrnd).. "&min="..tostring(ro.trnmin).. "&max="..tostring(ro.trnmax).. "&col=1&base=10&format=plain&rnd=new" network.request( url, "GET", function(event) if true == event.isError or nil == event.response or 200 ~= event.status then ro.rand = math.random ro.finit = false return end local rnds = {} for rand in string.gmatch(event.response, '([^\n]+)') do rnds[#rnds + 1] = tonumber(rand) end local numrnd = #rnds if 0 == ro.topidx then ro.rnds = {unpack(rnds)} else if ro.nxtidx > ro.topidx then local idx = 1 local trmidx = ro.nxtidx - 1 if numrnd < (trmidx - ro.topidx + 1) then trmidx = ro.topidx + numrnd - 1 end for i = ro.topidx, trmidx do ro.rnds[i] = rnds[idx] idx = idx + 1 end ro.topidx = trmidx + 1 if ro.nxtidx == ro.topidx then ro.topidx = 0 end elseif ro.nxtidx < ro.topidx then local idx = 1 local trmidx = ro.numrnd if numrnd < (trmidx - ro.topidx + 1) then trmidx = ro.topidx + numrnd - 1 end for i = ro.topidx, trmidx do ro.rnds[i] = rnds[idx] idx = idx + 1 end local updnum = trmidx - ro.topidx + 1 local trmidx = ro.nxtidx - 1 local numrem = numrnd - updnum if numrem < trmidx then trmidx = numrem end for i = 1, trmidx do ro.rnds[i] = rnds[idx] idx = idx + 1 end ro.topidx = trmidx + 1 if ro.nxtidx == ro.topidx then ro.topidx = 0 end end end ro.finit = false end) end ro.isInitialized = function() if math.random == ro.rand then return true end if #ro.rnds ~= ro.numrnd then return false end return true end ro.rand = function(min, max) if false == ro.isInitialized() then return end if ro.topidx == ro.nxtidx then return math.random(min, max) end local rnd = ro.rnds[ro.nxtidx] if 0 == ro.topidx then ro.topidx = ro.nxtidx end ro.nxtidx = ro.nxtidx + 1 if ro.numrnd < ro.nxtidx then ro.nxtidx = 1 end local usdnum = ro.nxtidx - ro.topidx if ro.topidx > ro.nxtidx then usdnum = ro.numrnd - ro.topidx + 1 usdnum = usdnum + ro.nxtidx - 1 end if (ro.numrnd / 2) < usdnum then ro.init(usdnum) end local unt = (ro.trnmax - ro.trnmin + 1) / (max - min + 1) local ret = math.floor(rnd / unt) + min return ret end return ro