Perfect Hash Generator
When the set of keys is known ahead of time and never changes — reserved words, column names, a fixed set of commands — a hash can be found that gives every one of them its own slot. No chains, no probing, no comparison after the lookup beyond confirming the key. The search is a search: try multipliers until one happens to separate every key. For a few dozen keys it usually takes only a handful of tries.
What this generator does
Tries random multipliers and shifts against your key list until one sends every key to a different slot, then shows the parameters and the finished table.
How to use this tool
- Paste your keys, one per line.
- Search, and read the multiplier and shift that separate them.
- Ask for no spare slots if you want the table exactly as large as the list.
- Copy the table out to build your lookup.
Understanding the controls
- Keys
- One per line, from two up to four hundred. Blank lines and repeats are ignored rather than counted, since a repeat is the same key twice.
- No spare slots
- Makes the table exactly as large as the key list. Such a hash is harder to find and leaves nowhere to add a key later, but wastes no space.
Common use cases
- A lookup table for a fixed set of keywords or commands
- Speeding up a parser that keeps comparing strings
- Building a switch over field names without a chain of comparisons
- Demonstrating what makes a hash function good or bad
- A compact table for an embedded target with no room for chains
How this generator works
The hash is the usual multiply-and-mix over the characters, with the multiplier and shift drawn at random until a set of parameters happens to separate every key. Before the result is shown, every key is hashed again from the stored parameters and checked for a repeat, checked against the slot the table claims, and checked to be inside the table at all.
Randomness and fairness
The multiplier and shift come from the browser's secure generator, so searching twice with the same keys usually gives different parameters. Both are equally valid.
For how randomness is produced across the whole site, see how Generate Random works.
Limitations and good to know
- It only holds for the exact key list given. Add a key and the hash may collide, so it must be found again.
- Keys are read as text and compared exactly, including case and spacing.
- The search is random rather than clever, so a very long list with no spare slots may not settle.
- The hash shown is a simple multiply-and-mix, not a cryptographic one, and must not be used where a hash needs to resist attack.
Privacy and your data
Your keys never leave your browser: the search runs on the page and nothing is transmitted or stored.
Related generators
- De Bruijn Sequence GeneratorOne sequence containing every possible run of a given length exactly once — 10,003 keypresses for all 10,000 four-digit codes.
- Damm Check Digit GeneratorAdds a check digit that catches every single mistyped digit and every swap of two neighbours, and proves it by trying them all.
- Hash Table Scenario GeneratorKeys distributed into buckets by modulo hashing, with collisions, chain lengths and empty buckets all recounted from the table.
- UUID GeneratorAn RFC 4122 v4 UUID for development or testing use — cryptographically random, not sequential.