summaryrefslogtreecommitdiffstats
path: root/rand.c
blob: da450f60d6cd176b51cfe7a2de1f1e8349af8124 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdlib.h>
#include <time.h>

#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;
}