#ifndef lib_rnd_h #define lib_rnd_h /* Cesar project {{{ * * Copyright (C) 2007 Spidcom * * <<>> * * }}} */ /** * \file lib/rnd.h * \brief Random Number Generator header. * \ingroup lib */ #include "config/rnd/mt19937.h" #include "config/rnd/tt800.h" /** Random number generator context. */ struct lib_rnd_t { #if CONFIG_RND_MT19937 /* MT19937 generator. */ #define LIB_RND_N 624 #else /* TT800 generator. */ #define LIB_RND_N 25 #endif unsigned long state[LIB_RND_N]; int state_index; }; typedef struct lib_rnd_t lib_rnd_t; /** Generate a integer which has a probability of \a prob to be greater than a * random 32 bit integer. Works with 0.0 <= \a prob < 1.0. */ #define LIB_RND_RATIO(prob) ((u32) ((1ull << 32) * (prob))) BEGIN_DECLS void lib_rnd_init (lib_rnd_t *ctx, u32 seed); #if CONFIG_RND_MT19937 void lib_rnd_init_by_array (lib_rnd_t *ctx, const u32 init_key[], int key_length); #endif u32 lib_rnd32 (lib_rnd_t *ctx); uint lib_rnd_uniform (lib_rnd_t *ctx, uint bound); void lib_rnd_buffer (lib_rnd_t *ctx, u8 *buf, uint buf_size); /** * Return true with the wanted probability. * \param ctx the rnd context * \param prob probability integer returned by LIB_RND_RATIO * \return true when lib_rnd32() < \p prob */ extern inline bool lib_rnd_flip_coin (lib_rnd_t *ctx, u32 prob) { return lib_rnd32 (ctx) < prob; } END_DECLS #endif /* lib_rnd_h */