Skip to content

Instantly share code, notes, and snippets.

@doujiang24
Created October 12, 2015 02:53
Show Gist options
  • Select an option

  • Save doujiang24/46dc48fa8a64e6265ec3 to your computer and use it in GitHub Desktop.

Select an option

Save doujiang24/46dc48fa8a64e6265ec3 to your computer and use it in GitHub Desktop.
crc32
local bit = require "bit"
local p20 = math.pow(2, 20)
local page = 1
local bor = bit.bor
local band = bit.band
local lshift = bit.lshift
local rshift = bit.rshift
local str_format = string.format
local tid = 0
local serverid = 0
local function _page_id(page)
return bor(lshift(band(page, 0x3ff), 10), lshift(band(tid, 0x1f), 5), serverid)
end
local function _session()
local uid = math.random(1, 50 * 10000)
page = (page + 1) % 1024
return str_format("%d", uid * p20 + _page_id(page))
end
local loop = 10000 * 50
local COUNT = {}
for i = 1, loop do
local s = _session()
local c = ngx.crc32_short(ngx.sha1_bin(s))
if COUNT[c] and COUNT[c] ~= s then
ngx.say(s, ":", COUNT[c], "; crc32: ", c)
end
COUNT[c] = s
end
@doujiang24
Copy link
Author

result:

290684877824:287517146112; crc32: 3294020946
185598192640:231815296000; crc32: 613731695
341109960704:361059556352; crc32: 3139542880
74066370560:15442535424; crc32: 3906529458
10227156992:122145448960; crc32: 3507125700
522782023680:447540187136; crc32: 1149085160
130948795392:486750537728; crc32: 3899721850
422093287424:76960457728; crc32: 1170199604
144943770624:406472268800; crc32: 2901879169
70984739840:119845049344; crc32: 1122488516
440109621248:461015337984; crc32: 1783937753
11402551296:237376423936; crc32: 988152192
76928073728:277234015232; crc32: 2380862528
437125331968:500618304512; crc32: 1597190189
244588553216:463525491712; crc32: 3923097524
238284492800:315555256320; crc32: 2018097524
509203231744:149924658176; crc32: 3885345313
205599260672:87266414592; crc32: 3205614162
113298075648:284513972224; crc32: 3118230336
507612646400:105540700160; crc32: 1065673762
194805218304:10452207616; crc32: 2309872391
316656210944:454764994560; crc32: 2208716088

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment