summaryrefslogtreecommitdiffstats
path: root/rand.cpp
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2024-10-09 17:00:53 -0400
committerSam Anthony <sam@samanthony.xyz>2024-10-09 17:00:53 -0400
commit20622555ee6eabb3d3c9f618d65b91736dd7097f (patch)
treeabe6faf05b22909c050449084e0c28f7158a6231 /rand.cpp
parentf48d24f99849fb9e801970bcb124cd49fa2418f6 (diff)
downloadballs-20622555ee6eabb3d3c9f618d65b91736dd7097f.zip
refactor
Diffstat (limited to 'rand.cpp')
-rw-r--r--rand.cpp31
1 files changed, 31 insertions, 0 deletions
diff --git a/rand.cpp b/rand.cpp
new file mode 100644
index 0000000..7a327ee
--- /dev/null
+++ b/rand.cpp
@@ -0,0 +1,31 @@
+#include "balls.h"
+
+double
+randDouble(double lo, double hi) {
+ double r, diff;
+ static int isInitialized = 0;
+
+ if (!isInitialized) { /* first call */
+ srand(time(0));
+ isInitialized = 1;
+ }
+
+ r = (double) rand() / (double) RAND_MAX;
+ diff = hi - lo;
+ return lo + r*diff;
+}
+
+Color
+randColor(void) {
+ Color color;
+
+ color.r = randDouble(0, 1);
+ color.g = randDouble(0, 1);
+ color.b = randDouble(0, 1);
+ return color;
+}
+
+Point
+randPtInRect(Rectangle r) {
+ return Pt(randDouble(r.min.x, r.max.x), randDouble(r.min.y, r.max.y));
+}