Hasard project
Hasard is a pseudo-random number generator (PRNG) library. It includes multiple engines (algorithms): Park-Miller, Mersenne Twister, Linux device (/dev/urandom or /dev/random), ... It has simple API but with strong code, eg. PRNG seed can be generated using strong entropy (hardware random number generator like /dev/random on Linux).
The library is written in C and a Python binding is available. The library is distributed under BSD license.
The word "hasard" is the french name of "randomness" or "chance".
Features
- Simple functions to get integer, float or boolean with the best distribution
- Support many engines: Mersenne Twister, ISAAC, Arcfour, ... (full list)
- No need to feed the initial seed with poor entropy (eg. srand(time(0))
- Functions to control the seed: jumpahead, get/set seed, get/set state and reseed
- Informations about the PRNG: min/max period, does it support reseed, seed or state?
- BSD license for easy integration in other libraries and programs
- Quality tests: test entropy, compute PRNG period, unit tests
- Attack tests: programs to find collisions, break a LCG
- Tools: programs to create images to "view" the random, plain text file format used by most test programs
Download
Stable version:
- hasard-0.5.0.tar.bz2
- MD5: 8308ee7bb8daeabd8c33e0fee64ab6a1
- SHA-1: 9f1093f7d2642efd04b36fb7e77169b885865fd0
Download development version using Mercurial:
hg clone http://haypo.hachoir.org/hasard
You can also browse the source code.
Version 0.5.0 works on Linux i386, Linux x86_64, Linux ppc and FreeBSD i386.
News
Hasard 0.5 (2008-07-11):
- Hasard is now thread safe: create locking callbacks
- Create random profiles: @fast, @secure, @hardware and @test
- Rewrite hasard_engine_int() and hasard_engine_ulong() to support 64 bits CPU: now use GMP library
- Create function hasard_uuid()
- Support FreeBSD, and partial support of Windows
- Switch to CMake build system
Hasard 0.4 (2008-06-17):
- Write get/set seed, reseed and get/set state functions
- hasard_double() now requires a range [min; max]
- New engines: Arcfour (RC4) and Middle-Square
- Add minimum/maximum period to the engine metadata
- Write many more tests
- Bugfixes for AMD64
Read also the ChangeLog.
Usage example
#include <hasard.h> #include <stdio.h> int main() { struct hasard_t *rnd; rnd = hasard_new(HASARD_FAST); printf("Heads or Tails? %s!\n", hasard_bool(rnd)?"Heads":"Tails"); printf("Dice: %i\n", hasard_int(rnd, 1, 6)); printf("Integer in 0..999: %u\n", hasard_ulong(rnd, 0, 999)); printf("Float in [0.0; 1.0]: %.3f\n", hasard_double(rnd)); hasard_destroy(rnd); return 0; }
Compile it with "gcc example.c -o example -lhasard".
You don't have to initialize the random generator seed like the common srand(time(NULL)): this task is done by hasard_new().
Documentation
- README
- Hasard engine list (engine_list.rst)
- Hasard API (hasard_api.rst)
- Engine API (engine_api.rst)
Tests
- Test entropy: tests/test_entropy.py
- Use ENT program: tests/ent.py
- Benchmark: benchmark.c
- Manual visual check: tests/gnuplot.py
- See hasard/graphics
Picture at the right is an image generated using Park-Miller engine. As you can see, it's not really very random...
Hardware entropy generators
Hardware:
- Sound: audio entropy daemon
- Video: video entropy daemon
- Lava Lamp! lavarnd.org
- True random numbers from Wi-Fi background noise
Daemons:
- EGD: Entropy Gathering Daemon
- PRNGD: Pseudo Random Number Generator Daemon
- aed: an "additional entropy" daemon for Linux
Other:
- Solaris /dev/random device (not maintained since 2002)
- Hurd /dev/random device
- rng-tools (part of gkernel project): inject entropy from hardware generator to /dev/random, but test also the entropy quality using FIPS 140-2
Download entropy from websites
- random.org
- HotBits: Genuine random numbers, generated by radioactive decay
- Quantum Random Bit Generator Service
- EntropyPool and Entropy Filter
Test RNG quality
Programs:
- DieHarder (2007) by Robert G. Brown
- ENT
- Diehard, Battery of Tests of Randomness (1995) by George Marsaglia, dedicated to RNG using 32 bits unsigned integer
- rngtest from the rng-tools package FIPS 140-2 (2001-10-10)
Documents:
- NIST 800-90 (2007): Recommendation for Random Number Generation Using Deterministic Random Bit Generators
- Be careful with Dual_EC_DRBG: read The Strange Story of Dual_EC_DRBG by Bruce Schneier
- NIST 800-22 (2001): A statistical test suite for random and pseudorandom number generators for cryptographic applications
- Software Generation of Practically Strong Random Numbers (1998, updated in 2000) by Peter Gutmann
- RFC 1750: Randomness Recommendations for Security (1994) by D. Eastlake, S. Crocker and J. Schiller
- Testing random number generators (1992) by Pierre L’Ecuyer
Other:
- Random Number Generation Technical Working Group from the NIST
Similar projects
- OpenSSL includes PRNG
- libgcrypt includes PRNG
- Pseudo-Random Number Generator (PRNG) by Otmar Lendl and Josef Leydold
- Python language includes its own random library written in Python (random.py)
- GNU Scientific Library (GSL)
- Boost Random Number Library
- The Scalable Parallel Random Number Generators Library (SPRNG)
- pLab-package: The pLab-package includes portable high-performance implementations of the linear congruential, quadratic congruential, inversive congruential, and explicit inversive congruential random number generators (LCG, QCG, ICG, and EICG, respectively), which were designed and implemented by Otmar Lendl.
Random links
- Computer Random vs. True Random (PHP, Windows and random.org)
- PHP rand(0,1) on Windows < OpenSSL rand() on Debian
- (fr) Comment générer des nombres aléatoires avec un ordinateur (HSC)
- Hasard library on Freshmeat
- (fr) Sortie de la bibliothèque Hasard version 0.2 (may 2008) on linuxfr.org
- (fr) Des Trappes dans les Clés (2003) by Eric Wegrzynowski
- (fr) Développement de la bibliothèque Hasard (june 2008)
- Entropy and Random Numbers (Henric Jungheim)
- Random Number Results (Bob Jenkins)

