What is a way to get a value that will be unique at start

Byakuren
Member
 
Posts: 441
Joined: Tue Apr 14, 2015 01:59
GitHub: raymoo
IRC: Hijiri

What is a way to get a value that will be unique at start

by Byakuren » Fri Sep 02, 2016 20:20

I'm looking for a way to generate unique IDs, and am going with the approach in http://antirez.com/news/99. Basically, I want to get a value on startup that is virtually guaranteed to be unique on startups, then append a counter to it to get unique IDs. For example, if on startup I generate the string "abcd", the generated IDs for that session would be "abcd-1", "abcd-2", and so on. I thought about using os.time, but that is implementation-dependent and might be broken and return the same thing each time, for example. The article I linked samples /dev/urandom, but that is both not portable and requires mod trust (for filesystem access). The method needs to be platform-independent, and ideally does not require mod trust.
Every time a mod API is left undocumented, a koala dies.
 

User avatar
rnd
Member
 
Posts: 136
Joined: Sun Dec 28, 2014 12:24
IRC: ac_minetest
In-game: rnd

Re: What is a way to get a value that will be unique at star

by rnd » Sat Sep 03, 2016 10:15

Math to the rescue..

Whats the probability to pick all different numbers from the numbers 1,...,n if you pick k times ?
Suppose each number is equally probable.


Then probability is:

n*(n - 1)* ...*(n - k + 1)/(n*n* ...*n) = 1*(1 - 1/n)* ...*(1 - (k - 1)/n) =
= approximately = E^(-k*(k - 1)/(2*n)) , since 1-x = aproximately = E^(-x)

So the probability of getting 2 same numbers is approximately : P=1 - E^(-k*(k - 1)/(2*n))

Some numbers:

case of k = 10 ^80 (we will select one key for every atom in known universe).
If we can select from n=2^512 keys (512 bit keys), probability that 2 will be same will be 1 so thats a fail but..

Take n = 2^600 (600 bit keys) and we get P = 1.204*10^-21, practically non existent


More realistic case, suppose we want to issue one key every second for (overexaggerated) 100 years, thats k = 3600*24*365*100. If we have 128 bit keys probability is 1.4613*10^-20 which is practically nonexistent.

In conclusion: if you have enough keys to choose from you are fine. Btw what that guy is saying, applying hash to key - that will decrease number of available keys since some hashes might be same. So unless you can prove that it doesnt drastically decrease thats ok, otherwise its equal to decreasing number of bits for keys, for how much? Good thing about it is you get different keys with different id: hash(id .. randomkey).

If you need good random numbers and dont trust generators - https://www.random.org/cgi-bin/randbyte ... 2&format=b. Then you can skip this hash thing too.
 

Byakuren
Member
 
Posts: 441
Joined: Tue Apr 14, 2015 01:59
GitHub: raymoo
IRC: Hijiri

Re: What is a way to get a value that will be unique at star

by Byakuren » Sat Sep 03, 2016 18:19

rnd: I need a good way to pick something, I already know how big my key space should be. I don't want to use an online source because it will require mod trust in secure mode to use the http API, and I want to avoid needing mod trust.
Every time a mod API is left undocumented, a koala dies.
 

Byakuren
Member
 
Posts: 441
Joined: Tue Apr 14, 2015 01:59
GitHub: raymoo
IRC: Hijiri

Re: What is a way to get a value that will be unique at star

by Byakuren » Sat Sep 03, 2016 20:18

I just noticed that I misunderstood his method, I thought the final key was 0foo, 1foo, 2foo, etc., not hashes of them. I'm planning on just generating one random thing at the beginning and appending a counter to it, without hashing.
Every time a mod API is left undocumented, a koala dies.
 


Return to Modding Discussion

Who is online

Users browsing this forum: No registered users and 5 guests

cron