Note that there are some explanatory texts on larger screens.

plurals
  1. POTinyurl-style unique code: potential algorithm to prevent collisions
    text
    copied!<p>I have a system that requires a unique 6-digit code to represent an object, and I'm trying to think of a good algorithm for generating them. Here are the pre-reqs:</p> <ul> <li>I'm using a base-20 system (no caps, numbers, vowels, or l to prevent confusion and naughty words) <ul> <li>The base-20 allows 64 million combinations</li> </ul></li> <li>I'll be inserting potentially 5-10 thousand entries at once, so in theory I'd use bulk inserts, which means using a unique key probably won't be efficient or pretty (especially if there starts being lots of collisions)</li> <li>It's not out of the question to fill up 10% of the combinations so there's a high potential for lots of collisions</li> <li>I want to make sure the codes are non-consecutive</li> </ul> <p>I had an idea that sounded like it would work, but I'm not good enough at math to figure out how to implement it: if I start at 0 and increment by N, then convert to base-20, it seems like there should be some value for N that lets me count each value from 0-63,999,999 before repeating any.</p> <p>For example, going from 0 through 9 using N=3 (so 10 mod 3): 0, 3, 6, 9, 2, 5, 8, 1, 4, 7.</p> <p>Is there some magic math method for figuring out values of N for some larger number that is able to count through the whole range without repeating? Ideally, the number I choose would sort of jump around the set such that it wasn't obvious that there was a pattern, but I'm not sure how possible that is.</p> <p>Alternatively, a hashing algorithm that guaranteed uniqueness for values 0-64 million would work, but I'm way too dumb to know if that's possible.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload