The get_probes() method generates a series of hashed lookups into the bitarray. There are many ways to do this and it is possible to get a much faster hashing function. The seeded random number generator approach was chosen for this recipe because 1) it produces as many uncorrelated bits as needed, 2) it is easy to understand, and 3) it works in both Python2 and Python3.

The example code shows the names of fifty US states stored in a 125-byte bloom filter using 14 probes. We verify that all 50 are found in the filter (guaranteed True Positives). We then try 100,000 random strings to see if they are in the filter. Typically, only a handful return a false positive.

Bloom filters are much more impressive with larger datasets. The above example also shows a list of all correctly spelled words in the English language compressed into a 330Kb bloom filter so that the spell checker can be kept entirely in memory.

For those interested in boosting performance, the above code includes example of BloomFilter subclasses with fast customized get_probes() methods. The first hardwires a 4Kb array, using 13 probes generated by the fast sha224 function. It performs well for up to 1,700 keys while keeping the false positive rate under 1 in 10,000. The second is a larger 32Kb array with 13 probes generated by sha256. It can hold up to 13,600 keys while keeping the false positive rate under 1 in 10,000. These arrays are compact enough to fit entirely in the L1 or L2 caches on modern processors (for example the Intel Sandy Bridge processors have a 32Kb L1 data cache and a 256Kb L2 cache).

The customized get_probes() methods can be further sped-up with traditional Python optimization techniques -- localize the global lookup of sha256() function, localize the builtin lookup for int(), and replace the range(13) call with a precomputed constant (or unroll the loop entirely):