Begins new random impl

This commit is contained in:
Linus Probert 2018-12-14 08:21:16 +01:00
parent 57b57c5051
commit 3c7dcb5ea1
2 changed files with 22 additions and 6 deletions

View File

@ -21,15 +21,28 @@
#include <stdbool.h> #include <stdbool.h>
#include "random.h" #include "random.h"
static unsigned int seed = 0;
static void
init_seed(void)
{
if (seed == 0) {
seed = (unsigned int) time(NULL);
srand(seed);
}
}
unsigned int
get_seed(void)
{
init_seed();
return seed;
}
unsigned int unsigned int
get_random(unsigned int max) get_random(unsigned int max)
{ {
static bool seeded = false; init_seed();
if (!seeded) {
srand((unsigned int) time(NULL));
seeded = true;
}
return rand() % (max + 1); return rand() % (max + 1);
} }

View File

@ -19,6 +19,9 @@
#ifndef RANDOM_H_ #ifndef RANDOM_H_
#define RANDOM_H_ #define RANDOM_H_
unsigned int
get_seed(void);
unsigned int unsigned int
get_random(unsigned int max); get_random(unsigned int max);