Monte-Carlo Rabin-Karp
When we hash a value by dividing it by a number, the resulting hash code will be some number from 0 up to (but excluding) that number itself. So if our chosen prime number is 613, the hash code will be some number from 0 through 612. Put another way, there are 613 possibilities as to what the final hash code will be.
It turns out that when a haystack window does not match the needle, there’s still a 1 out of 613 chance that their hash codes will match. If we choose a larger prime number, such as 7841, then each haystack window has a 1 in 7,841 chance of being incorrectly identified as a match to the needle.
Now, if our haystack is small relative to our needle so that we only have a few haystack windows, we’re unlikely to ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access