From dcca8a348a3816c5fbb3b9fa4b4451e9a5e100fa Mon Sep 17 00:00:00 2001 From: Edgaru089 Date: Tue, 26 Mar 2024 13:26:22 +0800 Subject: [PATCH] Very bad randomness --- types.c | 5 +++++ util/rand.c | 9 +++++++++ util/rand.h | 12 ++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 util/rand.c create mode 100644 util/rand.h 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