Cryptograhically random unique strings

前端 未结 4 1211
-上瘾入骨i
-上瘾入骨i 2021-01-13 15:00

In this answer, the below code was posted for creating unique random alphanumeric strings. Could someone clarify for me how exactly they are ensured to be unique in this cod

4条回答
  •  没有蜡笔的小新
    2021-01-13 16:04

    Uniqueness and randomness are mutually exclusive concepts. If a random number generator is truly random, then it can return the same value. If values are truly unique, although they may not be deterministic, they certainly aren't truly random, because every value generated removes a value from the pool of allowed values. This means that every run affects the outcome of subsequent runs, and at a certain point the pool is exhausted (barring of course the possibility of an infinitely-sized pool of allowed values, but the only way to avoid collisions in such a pool would be the use of a deterministic method of choosing values).

    The code you're showing generates values that are very random, but not 100% guaranteed to be unique. After enough runs, there will be a collision.

提交回复
热议问题