Skip to content

Commit ba9cd25

Browse files
committed
Add random_double(min,max) utility function.
1 parent f655b1c commit ba9cd25

File tree

13 files changed

+100
-93
lines changed

13 files changed

+100
-93
lines changed

books/RayTracingInOneWeekend.html

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -960,10 +960,16 @@
960960
...
961961

962962
inline double random_double() {
963+
// Returns a random real in [0,1).
963964
return rand() / (RAND_MAX + 1.0);
964965
}
966+
967+
inline double random_double(double min, double max) {
968+
// Returns a random real in [min,max).
969+
return min + (max-min)*random_double();
970+
}
965971
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
966-
[Listing [random-double]: <kbd>[rtweekend.h]</kbd> random_double() function]
972+
[Listing [random-double]: <kbd>[rtweekend.h]</kbd> random_double() functions]
967973
</div>
968974

969975
<div class='together'>
@@ -1162,7 +1168,7 @@
11621168
vec3 random_in_unit_sphere() {
11631169
vec3 p;
11641170
do {
1165-
p = 2.0*vec3(random_double(), random_double(), random_double()) - vec3(1,1,1);
1171+
p = vec3(random_double(-1,1), random_double(-1,1), random_double(-1,1));
11661172
} while (p.squared_length() >= 1.0);
11671173
return p;
11681174
}
@@ -1566,8 +1572,8 @@
15661572

15671573
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
15681574
vec3 random_unit_vector() {
1569-
auto a = 2*pi * random_double();
1570-
auto z = 2*random_double() - 1;
1575+
auto a = random_double(0, 2*pi);
1576+
auto z = random_double(-1, 1);
15711577
auto r = sqrt(1 - z*z);
15721578
return vec3(r*cos(a), r*sin(a), z);
15731579
}
@@ -2204,7 +2210,7 @@
22042210
vec3 random_in_unit_disk() {
22052211
vec3 p;
22062212
do {
2207-
p = 2.0*vec3(random_double(),random_double(),0) - vec3(1,1,0);
2213+
p = vec3(random_double(-1,1), random_double(-1,1), 0);
22082214
} while (dot(p,p) >= 1.0);
22092215
return p;
22102216
}
@@ -2303,7 +2309,7 @@
23032309
for (int a = -11; a < 11; a++) {
23042310
for (int b = -11; b < 11; b++) {
23052311
auto choose_mat = random_double();
2306-
vec3 center(a+0.9*random_double(),0.2,b+0.9*random_double());
2312+
vec3 center(a + 0.9*random_double(), 0.2, b + 0.9*random_double());
23072313
if ((center-vec3(4,0.2,0)).length() > 0.9) {
23082314
if (choose_mat < 0.8) { // diffuse
23092315
list[i++] = new sphere(center, 0.2,
@@ -2314,11 +2320,15 @@
23142320
);
23152321
}
23162322
else if (choose_mat < 0.95) { // metal
2317-
list[i++] = new sphere(center, 0.2,
2318-
new metal(vec3(0.5*(1 + random_double()),
2319-
0.5*(1 + random_double()),
2320-
0.5*(1 + random_double())),
2321-
0.5*random_double()));
2323+
list[i++] = new sphere(
2324+
center, 0.2,
2325+
new metal(
2326+
vec3(random_double(.5, 1),
2327+
random_double(.5, 1),
2328+
random_double(.5, 1)),
2329+
random_double(0, .5)
2330+
)
2331+
);
23222332
}
23232333
else { // glass
23242334
list[i++] = new sphere(center, 0.2, new dielectric(1.5));

books/RayTracingTheNextWeek.html

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,10 @@
135135
vec3 rd = lens_radius*random_in_unit_disk();
136136
vec3 offset = u * rd.x() + v * rd.y();
137137
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
138-
auto time = time0 + random_double()*(time1-time0);
139138
return ray(
140139
origin + offset,
141140
lower_left_corner + s*horizontal + t*vertical - origin - offset,
142-
time
141+
random_double(time0, time1)
143142
);
144143
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
145144
}
@@ -272,12 +271,12 @@
272271
for (int a = -10; a < 10; a++) {
273272
for (int b = -10; b < 10; b++) {
274273
auto choose_mat = random_double();
275-
vec3 center(a+0.9*random_double(),0.2,b+0.9*random_double());
276-
if ((center-vec3(4,0.2,0)).length() > 0.9) {
274+
vec3 center(a + 0.9*random_double(), 0.2, b + 0.9*random_double());
275+
if ((center - vec3(4, .2, 0)).length() > 0.9) {
277276
if (choose_mat < 0.8) { // diffuse
278277
list[i++] = new moving_sphere(
279278
center,
280-
center+vec3(0, 0.5*random_double(), 0),
279+
center + vec3(0, random_double(0, .5), 0),
281280
0.0, 1.0, 0.2,
282281
new lambertian(
283282
vec3(random_double()*random_double(),
@@ -290,10 +289,10 @@
290289
list[i++] = new sphere(
291290
center, 0.2,
292291
new metal(
293-
vec3(0.5*(1 + random_double()),
294-
0.5*(1 + random_double()),
295-
0.5*(1 + random_double())),
296-
0.5*random_double()
292+
vec3(random_double(.5, 1),
293+
random_double(.5, 1),
294+
random_double(.5, 1)),
295+
random_double(0, .5)
297296
)
298297
);
299298
}
@@ -768,7 +767,7 @@
768767

769768
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
770769
bvh_node::bvh_node(hittable **l, int n, double time0, double time1) {
771-
int axis = int(3*random_double());
770+
int axis = int(random_double(0,3));
772771

773772
if (axis == 0)
774773
qsort(l, n, sizeof(hittable *), box_x_compare);
@@ -1046,7 +1045,7 @@
10461045

10471046
void permute(int *p, int n) {
10481047
for (int i = n-1; i > 0; i--) {
1049-
int target = int(random_double()*(i+1));
1048+
int target = int(random_double(0, i+1));
10501049
int tmp = p[i];
10511050
p[i] = p[target];
10521051
p[target] = tmp;
@@ -1257,9 +1256,9 @@
12571256
static vec3* perlin_generate() {
12581257
vec3 *p = new vec3[256];
12591258
for (int i = 0; i < 256; ++i) {
1260-
double x_random = 2*random_double() - 1;
1261-
double y_random = 2*random_double() - 1;
1262-
double z_random = 2*random_double() - 1;
1259+
double x_random = random_double(-1,1);
1260+
double y_random = random_double(-1,1);
1261+
double z_random = random_double(-1,1);
12631262
p[i] = unit_vector(vec3(x_random, y_random, z_random));
12641263
}
12651264
return p;
@@ -2423,7 +2422,7 @@
24232422
auto z0 = -1000.0 + j*w;
24242423
auto y0 = 0.0;
24252424
auto x1 = x0 + w;
2426-
auto y1 = 100.0*(random_double()+0.01);
2425+
auto y1 = random_double(1,101);
24272426
auto z1 = z0 + w;
24282427
boxlist[b++] = new box(vec3(x0,y0,z0), vec3(x1,y1,z1), ground);
24292428
}
@@ -2455,8 +2454,7 @@
24552454
int ns = 1000;
24562455
for (int j = 0; j < ns; j++) {
24572456
boxlist2[j] = new sphere(
2458-
vec3(165*random_double(), 165*random_double(), 165*random_double()),
2459-
10, white);
2457+
vec3(random_double(0,165), random_double(0,165), random_double(0,165)), 10, white);
24602458
}
24612459

24622460
list[l++] = new translate(

books/RayTracingTheRestOfYourLife.html

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@
8383
int N = 1000;
8484
int inside_circle = 0;
8585
for (int i = 0; i < N; i++) {
86-
auto x = 2*random_double() - 1;
87-
auto y = 2*random_double() - 1;
88-
if(x*x + y*y < 1)
86+
auto x = random_double(-1,1);
87+
auto y = random_double(-1,1);
88+
if (x*x + y*y < 1)
8989
inside_circle++;
9090
}
9191
std::cout << std::fixed << std::setprecision(12);
@@ -115,8 +115,8 @@
115115
std::cout << std::fixed << std::setprecision(12);
116116
while (true) {
117117
runs++;
118-
auto x = 2*random_double() - 1;
119-
auto y = 2*random_double() - 1;
118+
auto x = random_double(-1,1);
119+
auto y = random_double(-1,1);
120120
if (x*x + y*y < 1)
121121
inside_circle++;
122122

@@ -156,8 +156,8 @@
156156
int sqrt_N = 10000;
157157
for (int i = 0; i < sqrt_N; i++) {
158158
for (int j = 0; j < sqrt_N; j++) {
159-
auto x = 2*random_double() - 1;
160-
auto y = 2*random_double() - 1;
159+
auto x = random_double(-1,1);
160+
auto y = random_double(-1,1);
161161
if (x*x + y*y < 1)
162162
inside_circle++;
163163
x = 2*((i + random_double()) / sqrt_N) - 1;
@@ -232,7 +232,7 @@
232232
int N = 1000000;
233233
double sum;
234234
for (int i = 0; i < N; i++) {
235-
auto x = 2*random_double();
235+
auto x = random_double(0,2);
236236
sum += x*x;
237237
}
238238
std::cout << std::fixed << std::setprecision(12);
@@ -417,7 +417,7 @@
417417
auto sum = 0.0;
418418
for (int i = 0; i < N; i++) {
419419
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
420-
auto x = sqrt(4*random_double());
420+
auto x = sqrt(random_double(0,4));
421421
sum += x*x / pdf(x);
422422
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
423423
}
@@ -435,7 +435,7 @@
435435

436436
<div class='together'>
437437
If we take that same code with uniform samples so the PDF = $1/2$ over the range [0,2] we can use
438-
the machinery to get `x = 2*random_double()` and the code is:
438+
the machinery to get `x = random_double(0,2)` and the code is:
439439

440440
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
441441
inline double pdf(double x) {
@@ -450,7 +450,7 @@
450450
auto sum = 0.0;
451451
for (int i = 0; i < N; i++) {
452452
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
453-
auto x = 2*random_double();
453+
auto x = random_double(0,2);
454454
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
455455
sum += x*x / pdf(x);
456456
}
@@ -497,7 +497,7 @@
497497
auto sum = 0.0;
498498
for (int i = 0; i < N; i++) {
499499
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
500-
auto x = pow(8*random_double(), 1./3.);
500+
auto x = pow(random_double(0,8), 1./3.);
501501
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
502502
sum += x*x / pdf(x);
503503
}
@@ -543,7 +543,7 @@
543543
vec3 random_in_unit_sphere() {
544544
vec3 p;
545545
do {
546-
p = 2*vec3(random_double(), random_double(), random_double()) - vec3(1,1,1);
546+
p = vec3(random_double(-1,1), random_double(-1,1), random_double(-1,1));
547547
} while (p.squared_length() >= 1);
548548
return p;
549549
}
@@ -1293,9 +1293,7 @@
12931293
return emitted;
12941294

12951295
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1296-
vec3 on_light = vec3(213 + random_double()*(343-213),
1297-
554,
1298-
227 + random_double()*(332-227));
1296+
vec3 on_light = vec3(random_double(213,343), 554, random_double(227,332));
12991297
vec3 to_light = on_light - rec.p;
13001298
auto distance_squared = to_light.squared_length();
13011299
to_light.make_unit_vector();
@@ -1534,9 +1532,9 @@
15341532
return true;
15351533
}
15361534
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++ highlight
1537-
virtual double pdf_value(const vec3& o, const vec3& v) const {
1535+
virtual double pdf_value(const vec3& origin, const vec3& v) const {
15381536
hit_record rec;
1539-
if (this->hit(ray(o, v), 0.001, infinity, rec)) {
1537+
if (this->hit(ray(origin, v), 0.001, infinity, rec)) {
15401538
auto area = (x1-x0)*(z1-z0);
15411539
auto distance_squared = rec.t * rec.t * v.squared_length();
15421540
auto cosine = fabs(dot(v, rec.normal) / v.length());
@@ -1545,12 +1543,9 @@
15451543
else
15461544
return 0;
15471545
}
1548-
virtual vec3 random(const vec3& o) const {
1549-
vec3 random_point = vec3(
1550-
x0 + random_double()*(x1-x0),
1551-
k,
1552-
z0 + random_double()*(z1-z0));
1553-
return random_point - o;
1546+
virtual vec3 random(const vec3& origin) const {
1547+
vec3 random_point = vec3(random_double(x0,x1), k, random_double(z0,z1));
1548+
return random_point - origin;
15541549
}
15551550
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
15561551
material *mp;

src/InOneWeekend/main.cc

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ hittable *random_scene() {
4444
for (int a = -11; a < 11; a++) {
4545
for (int b = -11; b < 11; b++) {
4646
auto choose_mat = random_double();
47-
vec3 center(a+0.9*random_double(),0.2,b+0.9*random_double());
48-
if ((center-vec3(4,0.2,0)).length() > 0.9) {
47+
vec3 center(a + 0.9*random_double(), 0.2, b + 0.9*random_double());
48+
if ((center - vec3(4, .2, 0)).length() > 0.9) {
4949
if (choose_mat < 0.8) { // diffuse
5050
list[i++] = new sphere(
5151
center, 0.2,
@@ -57,10 +57,12 @@ hittable *random_scene() {
5757
else if (choose_mat < 0.95) { // metal
5858
list[i++] = new sphere(
5959
center, 0.2,
60-
new metal(vec3(0.5*(1 + random_double()),
61-
0.5*(1 + random_double()),
62-
0.5*(1 + random_double())),
63-
0.5*random_double())
60+
new metal(
61+
vec3(random_double(.5, 1),
62+
random_double(.5, 1),
63+
random_double(.5, 1)),
64+
random_double(0, .5)
65+
)
6466
);
6567
}
6668
else { // glass

src/TheNextWeek/bvh.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ int box_z_compare (const void * a, const void * b)
8888
}
8989

9090
bvh_node::bvh_node(hittable **l, int n, double time0, double time1) {
91-
int axis = int(3*random_double());
91+
int axis = int(random_double(0,3));
9292

9393
if (axis == 0)
9494
qsort(l, n, sizeof(hittable *), box_x_compare);

0 commit comments

Comments
 (0)