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

Browse doc/ directory.

Tests

Park-Miller double() in 400x400 pixels

  • Test entropy: tests/test_entropy.py
  • Use ENT program: tests/ent.py
  • Benchmark: benchmark.c
  • Manual visual check: tests/gnuplot.py

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:

Daemons:

  • EGD: Entropy Gathering Daemon
  • PRNGD: Pseudo Random Number Generator Daemon
  • aed: an "additional entropy" daemon for Linux

Other:

Download entropy from websites

Test RNG quality

Programs:

Documents:

Other:

Similar projects