ocarina/include/core/random.h

47 lines
968 B
C

/**
* Copyright 2014 (c) Anna Schumaker
*/
#ifndef OCARINA_CORE_RANDOM_H
#define OCARINA_CORE_RANDOM_H
#ifdef CONFIG_TEST
void _seed_random(unsigned int);
unsigned int _pick_random();
#else /* CONFIG_TEST */
#include <stdlib.h>
static inline void _seed_random(unsigned int n) { srand(time(NULL) + n); }
static inline unsigned int _pick_random() { return rand(); }
#endif /* CONFIG_TEST */
/**
* Seed the random number generator.
*
* @param n Value used to initialize the RNG
*/
static inline void random_seed(unsigned int n)
{
_seed_random(n);
}
/**
* Get a random number in the specified range.
*
* @param min Minimum value that will be returned.
* @param max Maximum value that will be returned.
* @return A random value where min <= value <= max.
*/
static inline unsigned int random(unsigned int min, unsigned int max)
{
if (min >= max)
return min;
return min + (_pick_random() % (max - min));
}
#endif /* OCARINA_CORE_RANDOM_H */