# Unique value in GMOD?

Is there any function in LUA which would return unique (and not repeated further) value everytime I call it?
In PHP I would use uniqid with more_entropy flag. However, I could not find any analogs for this in LUA.

I had an idea of getting os.time(), converting it to string and attaching value from math.random(-99999, 99999) 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

Why exactly simply os.time() or even gUniquieNumber = 0 gUniquieNumber = gUniquieNumber + 1 doesn’t suit you?

Thanks for reply. os.time() 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.

Lol. Beauty in simplicity. I would try that. Thanks ;3

``````
local function UniqueValue()
local value = ""

for i = 1, 50 do
value = value .. string.char(math.random(32, 126))
end

return util.CRC(value)
end

``````

I would say this generates some pretty unique values.

It takes a long time and it doesn’t guarantee uniqueness. It’s the same problem Player:UniqueID has.

``````
local function UniqueValue()
value = ""

for i = 1, 9999999999999 do
value = value .. tostring(math.random(9))
end

end

``````

Is this any better? :^)

Yeah it’s totally efficient now

Why not use math.huge, that would be even better!

You can use MD5 unique values: https://github.com/Kefta/GSWeapons/blob/master/gsweapons_base/lua/code_gs/gsweapons_dash.lua#L2171-L2257

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

``````

025869925
847939350
080367341
973085467
544541033
819431690

``````

That can definitely create collisions. This has already been suggested in the thread

Or just… you know… as said before…

``````

local id = 0

local function CreateID()
id = id + 1
return id
end

``````

[editline]24th October 2016[/editline]

If you want it to be stored then just save it in a serverside text file