aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/rtw/rand.zig
diff options
context:
space:
mode:
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();
+}