• Unique value in GMOD?
    12 replies, posted
Is there any function in LUA which would return unique (and not repeated further) value everytime I call it? In PHP I would use [URL="http://php.net/manual/ru/function.uniqid.php"]uniqid[/URL] with more_entropy flag. However, I could not find any analogs for this in LUA. I had an idea of getting [I]os.time()[/I], converting it to string and attaching value from [I]math.random(-99999, 99999)[/I] to the string but this looks retarded for me, huh. Or is that correct way? I ain't gonna use for cryptography, but for identifying temporary records in table, if anyone is interested in that :3 Thanks in advance ;)
Why exactly simply os.time() or even gUniquieNumber = 0 gUniquieNumber = gUniquieNumber + 1 doesn't suit you?
[QUOTE=Robotboy655;51246916]Why exactly simply os.time() or even gUniquieNumber = 0 gUniquieNumber = gUniquieNumber + 1 doesn't suit you?[/QUOTE] [QUOTE]os.time()[/QUOTE] Thanks for reply. [I]os.time()[/I] does not suit me because the function which makes temporary records could be ran multiple times a second. So if it was called ie 3 times in the same second my table would have 3 different records which would be misidentified as the same ones. [QUOTE]gUniquieNumber = 0 gUniquieNumber = gUniquieNumber + 1[/QUOTE] Lol. Beauty in simplicity. I would try that. Thanks ;3
[code]local function UniqueValue() local value = "" for i = 1, 50 do value = value .. string.char(math.random(32, 126)) end return util.CRC(value) end[/code] I would say this generates some pretty unique values.
[QUOTE=Moosicorn;51248076][code]local function UniqueValue() local value = "" for i = 1, 50 do value = value .. string.char(math.random(32, 126)) end return util.CRC(value) end[/code] I would say this generates some pretty unique values.[/QUOTE] It takes a long time and it doesn't guarantee uniqueness. It's the same problem [url=https://wiki.garrysmod.com/page/Player/UniqueID]Player:UniqueID[/url] has.
[QUOTE=NeatNit;51248187]It takes a long time and it doesn't guarantee uniqueness. It's the same problem [url=https://wiki.garrysmod.com/page/Player/UniqueID]Player:UniqueID[/url] has.[/QUOTE] [code]local function UniqueValue() value = "" for i = 1, 9999999999999 do value = value .. tostring(math.random(9)) end return tonumber(value) end[/code] Is this any better? :^)
Yeah it's totally efficient now :D
[QUOTE=Moosicorn;51248243][code]local function UniqueValue() value = "" for i = 1, 9999999999999 do value = value .. tostring(math.random(9)) end return tonumber(value) end[/code] Is this any better? :^)[/QUOTE] Why not use math.huge, that would be even better!
You can use MD5 unique values: [url]https://github.com/Kefta/GSWeapons/blob/master/gsweapons_base/lua/code_gs/gsweapons_dash.lua#L2171-L2257[/url]
All hashing functions have a chance for collisions. That's just the way it works - you're using a fixed, finite number of bits. Sooner or later you're going to have two pieces of data with the same checksum, and it's going to happen way sooner than the number of possible values in those bits. Sure, it's a bit of a tinfoil-hat thing, but it's technically true. The chance is there, however statistically insignificant. In situation where a running index is acceptable (1, 2, 3, ...) it should be used, just as Robotboy655 said, and OP accepted. [editline]24th October 2016[/editline] SQL Server table columns can be set as "Identity", which basically does just that - increase the value in this column by one for every new record. Shitty name for a function like that (what the fuck does "Identity" mean in this context? Identity sounds like a synonym for Primary Key!) but whatever. I think other SQL software has similar functionality but I don't know if it's called the same.
[lua] local reps = 9 local stored = {} local random = math.random local function CreateID() local id = "" for i=1,reps do id = id .. random( 0, 9 ) end return id end function RegisterID() local id = tonumber( CreateID() ) if stored[ id ] then RegisterID() return end stored[ id ] = true return id end [/lua] Output examples [code] 025869925 847939350 080367341 973085467 544541033 819431690 [/code]
[QUOTE=Derek_SM;51251076][lua] local reps = 9 local stored = {} local random = math.random local function CreateID() local id = "" for i=1,reps do id = id .. random( 0, 9 ) end return id end function RegisterID() local id = tonumber( CreateID() ) if stored[ id ] then RegisterID() return end stored[ id ] = true return id end [/lua] Output examples [code] 025869925 847939350 080367341 973085467 544541033 819431690 [/code][/QUOTE] That can definitely create collisions. This has already been suggested in the thread
Or just... you know... as said before... [CODE] local id = 0 local function CreateID() id = id + 1 return id end [/CODE] [editline]24th October 2016[/editline] If you want it to be stored then just save it in a serverside text file
Sorry, you need to Log In to post a reply to this thread.