/******************************************************************/ /* 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 #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::max()) { std::uniform_int_distribution dist(0, up); return dist(rnd); } static float random_float(float up=1.0f) { std::uniform_real_distribution 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_ */