view Utility.lua @ 3:a55f4f2a4603 tip

intial (real) commit!
author Kyle@Kyle-PC
date Thu, 23 Feb 2012 00:34:43 -0500
parents
children
line wrap: on
line source
--[[ 
Title: Suicide Kings
Author: Brom
Version: 0.1
]]

local function RandomCharacter()
    
    -- 0-9, A-Z, a-z = 10 + 26 + 16
    local i = math.random(0,61)
    
    -- strip offset and ascii jump
    if ( i >= 36 ) then
        -- a:z
        i = ( i - 36 ) + 97
    elseif ( i >= 10 ) then
        -- A:Z
        i = ( i - 10 ) + 65
    else
        -- 0:9
        i = ( i + 48 )
    end
    
    return string.char( i )

end

function SuicideKings:UID( reserved )
    
    -- uid length
    -- unique values = 62^4 = 14776336
    local length = 4
    
    -- generate a random string
    local s = RandomCharacter()
    for i = 1, length-1 do
        local c = RandomCharacter()
        s = s .. c
    end
    
    -- if taken, try again
    while ( reserved[s] ~= nil ) do
        s = SuicideKings:UID( reserved )
    end
    
    return s
    
end