From 93a3f005480effe48513e7bc81161db1adc43f32 Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Mon, 28 Oct 2024 11:09:12 -0400 Subject: move --- rand.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 rand.c (limited to 'rand.c') diff --git a/rand.c b/rand.c new file mode 100644 index 0000000..da450f6 --- /dev/null +++ b/rand.c @@ -0,0 +1,36 @@ +#include +#include + +#include "balls.h" + +static float randFloat(float lo, float hi); + +float2 +randPtInRect(Rectangle r) { + float2 pt = { + randFloat(r.min[0], r.max[0]), + randFloat(r.min[1], r.max[1]) + }; + return pt; +} + +float2 +randVec(float xmin, float xmax, float ymin, float ymax) { + float2 v = {randFloat(xmin, xmax), randFloat(ymin, ymax)}; + return v; +} + +static float +randFloat(float lo, float hi) { + float r, diff; + static int isInitialized = 0; + + if (!isInitialized) { /* First call. */ + srand(time(0)); + isInitialized = 1; + } + + r = (float) rand() / RAND_MAX; + diff = hi - lo; + return lo + r*diff; +} -- cgit v1.2.3