Skip to content

A simple, time-tested, family of random hash functions in Java, based on CRC32, affine transformations, and the Mersenne Twister. 🎲

License

Notifications You must be signed in to change notification settings

jlumbroso/java-random-hash

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Java randomhash package

A simple, time-tested, family of random hash functions in Java, based on CRC32, affine transformations, and the Mersenne Twister.

This is a companion library to the identical Python version.

Installation

A JAR file for this library can be downloaded from this repo.

Alternatively, this library can also be installed with Apache Maven. First, add the following to the dependencies section of your pom.xml:

<dependency>
  <groupId>edu.princeton.cs.randomhash</groupId>
  <artifactId>randomhash</artifactId>
  <version>1.0.0</version>
</dependency>

Then run Maven:

$ mvn install

Features

This introduces a family of hash functions that can be used to implement probabilistic algorithms such as HyperLogLog. It is based on affine transformations of the CRC32 hash functions, which have been empirically shown to provide good performance. The pseudo-random numbers are drawn according to David Beaumont's Java implementation (included here for convenience but with full credit) of the Mersenne Twister.

To try out the hash functions, you can compile and run the example program:

javac -cp 'randomhash-1.0.0.jar:.' Example.java
java -cp 'randomhash-1.0.0.jar:.' Example

This will generate a report, such as the one below, which shows how a hundred hash functions perform on provided data that appears pseudo-random (note that it is important when running these audits that the data provide as input be made of unique elements, even if the hash functions will mainly be used in streaming algorithms, to project duplicates to the same hashed value):

java Example
input: data/unique.txt
number of hash functions: 100
hashing report:
> bucket count: 10
> total values hashed: 1670700
> [ 10.00% 10.03% 10.03%  9.96% 10.01% 10.00%  9.99%  9.98% 10.02%  9.98%  ]
> chi^2 test: 0.000399
> is uniform (with 90% confidence)? true

In practice, you can use it this way, by instantiating a family and using the hash(String) method to generate a single hashed value:

import randomhash.RandomHashFamily;

RandomHashFamily rhf = new RandomHashFamily(1);

System.out.print("hello -> ");
System.out.print(rhf.hash("hello"));

which will print:

hello -> 2852342977

and it can also generate several pseudo-random hash values at the same time, in this case 10, which it will return in an array:

RandomHashFamily rhf = new RandomHashFamily(10);
long[] hashes = rhf.hashes(); // 10 elements

Some history

In 1983, G. N. N. Martin and Philippe Flajolet introduced the algorithm known as Probabilistic Counting, designed to provide an extremely accurate and efficient estimate of the number of unique words from a document that may contain repetitions. This was an incredibly important algorithm, which introduced a revolutionary idea at the time:

The only assumption made is that records can be hashed in a suitably pseudo-uniform manner. This does not however appear to be a severe limitation since empirical studies on large industrial files [5] reveal that careful implementations of standard hashing techniques do achieve practically uniformity of hashed values.

The idea is that hash functions can "transform" data into pseudo-random variables. Then a text can be treated as a sequence of random variables drawn from a uniform distribution, where a given word will always occur as the same random value (i.e., a b c a a b c could be hashed as .00889 .31423 .70893 .00889 .00889 .31423 .70893 with every occurrence of a hashing to the same value). While this sounds strange, empirical evidence suggests it is true enough in practice, and eventually some theoretical basis has come to support the practice.

The original Probabilistic Counting (1983) algorithm gave way to LogLog (2004), and then eventually HyperLogLog (2007), one of the most famous algorithms in the world as described in this article. These algorithms and others all used the same idea of hashing inputs to treat them as random variables, and proved remarkably efficient and accurate.

But as highlighted in the above passage, it is important to be careful.

Hash functions in practice

In practice, it is easy to use poor quality hash functions, or to use cryptographic functions which will significantly slow down the speed (and relevance) of the probabilistic estimates. However, on most data, some the cyclic polynomial checksums (such as Adler32 or CRC32) provide good results.

Reference on creating a package with Maven

About

A simple, time-tested, family of random hash functions in Java, based on CRC32, affine transformations, and the Mersenne Twister. 🎲

Topics

Resources

License

Stars

Watchers

Forks