Snowfakery icon indicating copy to clipboard operation
Snowfakery copied to clipboard

Function for generating unique alphanumeric codes

Open prescod opened this issue 4 years ago • 0 comments

Goal:

  • be able to generate N-char strings limited to a particular character set (usually A-Z0-9, but perhaps also a-zA-Z0-9 or RFC 4648 Base32 or ...)
  • preferable: have them seem random.

Strategy:

  • "globally unique context number" for the whole run passed into Snowfakery (represents a node, process_id, thead_id, etc.)
  • "locally unique" counter increments in the interpreter
  • each can be translated into strings using Base36

Making them random strategy 1:

  • A reversible shuffle can be used to do the permuting and reverse permuting. Python's built-in shuffle is supposed to be fischer-yates (reversible)
  • Fischer-Yates needs a "key". We can generate the key from a hash of the value. We can make the key small enough by truncating it using modulus (hash(value)%36). In Python, this would be the "seed" of Random()
  • If we append the hash to the value then the operation becomes reversible. (whether we write the reversal algorithm or not). It might be nice to write the reversal algorithm to check the theory that the function is reversible. https://stackoverflow.com/a/54726084/113477
  • If the operation is reversible then it must be a bijection: https://en.wikipedia.org/wiki/Bijection

Making them random strategy 2:

  • render the characters to a bitstring
  • perform some kind of repeatable shuffle on the bits. (always the exact same shuffle)
  • render back to a charstring
  • https://www.delftstack.com/howto/python/string-to-binary-python/

References:

  • https://en.wikipedia.org/wiki/Base36
  • https://stackoverflow.com/questions/1181919/python-base-36-encoding
  • Truncated hash: hash(foo) % 36
  • Use that hash as a key
  • Reversible Shuffle (Fischer-Yates): https://stackoverflow.com/questions/3541378/reversible-shuffle-algorithm-using-a-key
  • The hash itself can be encoded as a Base36 char and appended to the string

prescod avatar Jun 11 '21 19:06 prescod