aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/rtw/rand.zig
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2022-12-07 22:37:14 +0900
committernsfisis <nsfisis@gmail.com>2022-12-07 22:54:47 +0900
commit86914985b77ddf5d6d3a3dd6c13deed9d906a471 (patch)
treea523883d6524d55aee713bfd6400f044f8715d52 /src/rtw/rand.zig
parente264078ed9d94b5091a6d78f8128496ac49fbde4 (diff)
downloadRayTracingInOneWeekend.zig-86914985b77ddf5d6d3a3dd6c13deed9d906a471.tar.gz
RayTracingInOneWeekend.zig-86914985b77ddf5d6d3a3dd6c13deed9d906a471.tar.zst
RayTracingInOneWeekend.zig-86914985b77ddf5d6d3a3dd6c13deed9d906a471.zip
refactor: separate single main.zig
Diffstat (limited to 'src/rtw/rand.zig')
-rw-r--r--src/rtw/rand.zig40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/rtw/rand.zig b/src/rtw/rand.zig
new file mode 100644
index 0000000..47a8d61
--- /dev/null
+++ b/src/rtw/rand.zig
@@ -0,0 +1,40 @@
+const std = @import("std");
+
+const Vec3 = @import("vec.zig").Vec3;
+
+pub const Random = std.rand.Random;
+
+// [min, max)
+pub fn randomInt(comptime T: type, rand: Random, min: T, max: T) T {
+ return rand.intRangeLessThan(T, min, max);
+}
+
+// [0, 1)
+pub fn randomReal01(rand: Random) f64 {
+ return rand.float(f64);
+}
+
+// [min, max)
+pub fn randomReal(rand: Random, min: f64, max: f64) f64 {
+ return min + randomReal01(rand) * (max - min);
+}
+
+pub fn randomPointInUnitSphere(rand: Random) Vec3 {
+ while (true) {
+ const p = Vec3.random(rand, -1.0, 1.0);
+ if (p.norm() >= 1) continue;
+ return p;
+ }
+}
+
+pub fn randomPointInUnitDisk(rand: Random) Vec3 {
+ while (true) {
+ const p = Vec3{ .x = randomReal(rand, -1.0, 1.0), .y = randomReal(rand, -1.0, 1.0), .z = 0.0 };
+ if (p.norm() >= 1) continue;
+ return p;
+ }
+}
+
+pub fn randomUnitVector(rand: Random) Vec3 {
+ return randomPointInUnitSphere(rand).normalized();
+}