diff --git a/types.c b/types.c index 4f03a4e..6c784cc 100644 --- a/types.c +++ b/types.c @@ -1,5 +1,6 @@ #include "types.h" +#include "util/rand.h" #include #include #include @@ -41,6 +42,10 @@ Vec2 vec2_Scale(Vec2 v, double scale) { } Vec2 vec2_Random(double minX, double maxX, double minY, double maxY) { + Vec2 result = { + .x = minX + (maxX - minX) * rand_Double01(), + .y = minY + (maxY - minY) * rand_Double01()}; + return result; } diff --git a/util/rand.c b/util/rand.c new file mode 100644 index 0000000..ebd4819 --- /dev/null +++ b/util/rand.c @@ -0,0 +1,9 @@ + +#include "rand.h" +#include + + +double rand_Double01() { + // Very bad implementation. + return ((double)rand()) / (double)RAND_MAX; +} diff --git a/util/rand.h b/util/rand.h new file mode 100644 index 0000000..c7e3ebd --- /dev/null +++ b/util/rand.h @@ -0,0 +1,12 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +// returns a double in the [0,1) range. +double rand_Double01(); + +#ifdef __cplusplus +} +#endif