-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom.h
More file actions
72 lines (62 loc) · 1.72 KB
/
random.h
File metadata and controls
72 lines (62 loc) · 1.72 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#pragma once
#include <random>
#include "vec3.h"
namespace rtx {
class random {
public:
random(int seed) : distribution_(0.0, 1.0), generator_(seed) {}
random() : distribution_(0.0, 1.0), generator_(5489u) {}
float random_float() { return distribution_(generator_); }
float random_float(float min, float max) {
// Returns a random real in [min,max).
return min + (max - min) * random_float();
}
int random_int(int min, int max) {
return min + (max - min) * random_float();
}
vec3 random_vec3() {
return vec3(random_float(), random_float(), random_float());
}
vec3 random_vec3(float min, float max) {
return vec3(random_float(min, max), random_float(min, max),
random_float(min, max));
}
vec3 random_in_unit_sphere() {
while (true) {
vec3 p = random_vec3(-1, 1);
if (p.length_squared() >= 1) {
continue;
}
return p;
}
}
vec3 random_in_unit_disk() {
while (true) {
vec3 p = vec3(random_float(-1, 1), random_float(-1, 1), 0);
if (p.length_squared() >= 1) {
continue;
}
return p;
}
}
vec3 random_unit_vector() {
// Lambertian's cosine law.
float angle = random_float(0, 2 * pi);
float z = random_float(-1, 1);
float radiance = sqrt(1 - z * z);
return vec3(radiance * cos(angle), radiance * sin(angle), z);
}
vec3 random_in_hemisphere(const vec3 &normal) {
vec3 in_unit_sphere = random_in_unit_sphere();
if (dot(in_unit_sphere, normal) > 0.0) {
// In the same hemisphere as the normal
return in_unit_sphere;
} else {
return -in_unit_sphere;
}
}
private:
std::uniform_real_distribution<float> distribution_;
std::mt19937 generator_;
};
}