37 lines
643 B
C
37 lines
643 B
C
|
/*
|
||
|
* Copyright 2014 (c) Anna Schumaker
|
||
|
*/
|
||
|
|
||
|
#ifndef OCARINA_RANDOM_H
|
||
|
#define OCARINA_RANDOM_H
|
||
|
|
||
|
#ifndef 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(); }
|
||
|
|
||
|
#else /* CONFIG_TEST */
|
||
|
|
||
|
void _seed_random(unsigned int);
|
||
|
unsigned int _pick_random();
|
||
|
|
||
|
#endif /* CONFIG_TEST */
|
||
|
|
||
|
|
||
|
|
||
|
static inline void random_seed(unsigned int n)
|
||
|
{
|
||
|
_seed_random(n);
|
||
|
}
|
||
|
|
||
|
static inline unsigned int random(unsigned int min, unsigned int max)
|
||
|
{
|
||
|
if (min >= max)
|
||
|
return min;
|
||
|
return min + (_pick_random() % (max - min));
|
||
|
}
|
||
|
|
||
|
|
||
|
#endif /* OCARINA_RANDOM_H */
|