Pseudo-Random Number Generator using SHA-256

Input a random seed with at least 20 digits (generated by rolling a 10-sided die, for instance), the number of objects from which you want a sample, and the number of objects you want in the sample.

Pseudo-Random Sample Using SHA-256



What this does

The "seed," concatenated with a comma and the "Sample number," is passed through the SHA-256 hash function. The result is displayed as "Hashed value (for testing)". The hashed value, interpreted as a hexadecimal number, is divided by "Number of objects from which to sample." One is added to the remainder of that division to get "Randomly selected item," which will be a number between 1 and "Number of objects from which to sample," inclusive. Clicking "draw sample" successively adds one to "Sample number" and recomputes "Hashed value" and "Randomly selected item" "Draw this many objects" times. Selected items accumulate in "Items selected" (and "Sorted items selected"), which reset if the seed or the number of objects changes. The same item might be selected more than once. Duplicates are removed in the "Sorted items selected, duplicates removed." Clicking the "reset" button clears the history but leaves the seed.

I learned about this method of generating pseudo-random numbers from Ronald L. Rivest. It is related to a method described in https://tools.ietf.org/html/rfc3797. The SHA-256 hash algorithm produces hash values that are hard to predict from the input. They are also roughly equidistributed as the input varies. The advantages of this approach for election auditing and some other applications include the following:

For comparison, a reference implementation of this approach in Python written by Ronald L. Rivest is available at https://people.csail.mit.edu/rivest/sampler.py.

P.B. Stark, statistics.berkeley.edu/~stark. https://statistics.berkeley.edu/~stark/Java/Html/auditRand.htm Last modified 11 January 2017.