diff options
Diffstat (limited to 'rand.cpp')
| -rw-r--r-- | rand.cpp | 31 |
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)); +} |