summaryrefslogtreecommitdiff
path: root/hw6/include/random_number.h
diff options
context:
space:
mode:
author53hornet <53hornet@gmail.com>2019-02-02 23:33:15 -0500
committer53hornet <53hornet@gmail.com>2019-02-02 23:33:15 -0500
commitdb072ad4dc181eca5a1458656b130beb43f475bf (patch)
treea3c03c7f5497cb70503e2486662fa85cfb53415a /hw6/include/random_number.h
downloadcsci427-db072ad4dc181eca5a1458656b130beb43f475bf.tar.xz
csci427-db072ad4dc181eca5a1458656b130beb43f475bf.zip
Diffstat (limited to 'hw6/include/random_number.h')
-rw-r--r--hw6/include/random_number.h50
1 files changed, 50 insertions, 0 deletions
diff --git a/hw6/include/random_number.h b/hw6/include/random_number.h
new file mode 100644
index 0000000..328de25
--- /dev/null
+++ b/hw6/include/random_number.h
@@ -0,0 +1,50 @@
+/******************************************************************/
+/* This file is part of the homework assignments for CSCI-427/527 */
+/* at The College of William & Mary and authored by Pieter Peers. */
+/* No part of this file, whether altered or in original form, can */
+/* be distributed or used outside the context of CSCI-427/527 */
+/* without consent of either the College of William & Mary or */
+/* Pieter Peers. */
+/******************************************************************/
+#ifndef _RANDOM_NUMBER_H_
+#define _RANDOM_NUMBER_H_
+
+#include <random>
+#include "vec3d.h"
+#include "constants.h"
+#include "coordinateTransform.h"
+
+static std::random_device rd;
+static std::mt19937 rnd;
+
+static unsigned int random_int(unsigned int up=std::numeric_limits<unsigned int>::max())
+{
+ std::uniform_int_distribution<unsigned int> dist(0, up);
+ return dist(rnd);
+}
+
+
+static float random_float(float up=1.0f)
+{
+ std::uniform_real_distribution<float> dist(0, up);
+ return dist(rnd);
+}
+
+
+static vec3d random_direction(const vec3d& normal, float r1=random_float(), float r2=random_float())
+{
+ // generate in standard coordinate system (i.e., Z=(0,0,1))
+ vec3d out(cos(2.0f*PI*r1) * sqrt(std::max(1.0f-r2*r2, 0.0f)),
+ sin(2.0f*PI*r1) * sqrt(std::max(1.0f-r2*r2, 0.0f)),
+ r2);
+
+ // transform to global coordinates
+ coordinateTransformation trans(normal);
+ out = trans.transformDirection(out);
+
+ // Done (PDF = 1/(2.0f*PI))
+ return out;
+}
+
+
+#endif /* _RANDOM_NUMBER_H_ */