Skip to content

Instantly share code, notes, and snippets.

@yunyyyun
Last active August 23, 2017 07:21
Show Gist options
  • Save yunyyyun/1226ffb0c1098d9ecbc9a779880e814c to your computer and use it in GitHub Desktop.
Save yunyyyun/1226ffb0c1098d9ecbc9a779880e814c to your computer and use it in GitHub Desktop.
一种均匀散列方法(hash)
g_HashTable = 0
--创建hash表
function createHashTable()
local tb = {};
local _insert = function(k,v)
if k==0 then
k=1;
end
for i = k,k+999,1 do
if i>1000 then
i = i-1000;
end
if tb[i]==nil then
tb[i]=v;
if i == 0 then
print(i,k,v)
end
return;
end
end
end
for i = 1,1000,1 do
local tmp = 131313*i+1113;
local key = tonumber(string.sub(tmp,-4,-2)) or 1;
_insert(key,i)
end
return tb;
end
--校验hash表
function judeHashTable()
local a=0;
local b=0;
if g_HashTable==0 then
g_HashTable = createHashTable()
end
for i,v in pairs(g_HashTable) do
a=a+i;
b=b+v;
print(i,v)
end
print(a,b)
end
--指定uid和命中概率q,判断用户是否命中,要求同一输入,输出唯一确定
function grayRelease(uid,q)
local midNum = tonumber(string.sub(uid,-4,-2)) or 0;
if g_HashTable==0 then
g_HashTable = createHashTable()
end
local hashValue = g_HashTable[midNum+1]
if hashValue<=1000*q then
return 1;
end
return 0;
end
--test,样本uid:242368487~242368487+200000-1
for i,v in pairs(arg) do
if type(tonumber(v))=="number" then
local count = 0;
local n1 = 242368487;
--local n2 = 259643522;
for i=n1,n1+200000-1,1 do
count = count + grayRelease(i,v)
end
print(v,count,count/(200000))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment