Path: blob/master/3rdparty/openexr/Imath/ImathRandom.h
16337 views
///////////////////////////////////////////////////////////////////////////1//2// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas3// Digital Ltd. LLC4//5// All rights reserved.6//7// Redistribution and use in source and binary forms, with or without8// modification, are permitted provided that the following conditions are9// met:10// * Redistributions of source code must retain the above copyright11// notice, this list of conditions and the following disclaimer.12// * Redistributions in binary form must reproduce the above13// copyright notice, this list of conditions and the following disclaimer14// in the documentation and/or other materials provided with the15// distribution.16// * Neither the name of Industrial Light & Magic nor the names of17// its contributors may be used to endorse or promote products derived18// from this software without specific prior written permission.19//20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.31//32///////////////////////////////////////////////////////////////////////////333435#ifndef INCLUDED_IMATHRANDOM_H36#define INCLUDED_IMATHRANDOM_H3738//-----------------------------------------------------------------------------39//40// Generators for uniformly distributed pseudo-random numbers and41// functions that use those generators to generate numbers with42// non-uniform distributions:43//44// class Rand3245// class Rand4846// solidSphereRand()47// hollowSphereRand()48// gaussRand()49// gaussSphereRand()50//51// Note: class Rand48() calls erand48() and nrand48(), which are not52// available on all operating systems. For compatibility we include53// our own versions of erand48() and nrand48(). Our functions have54// been reverse-engineered from the corresponding Unix/Linux man page.55//56//-----------------------------------------------------------------------------5758#include <stdlib.h>59#include <math.h>6061namespace Imath {6263//-----------------------------------------------64// Fast random-number generator that generates65// a uniformly distributed sequence with a period66// length of 2^32.67//-----------------------------------------------6869class Rand3270{71public:7273//------------74// Constructor75//------------7677Rand32 (unsigned long int seed = 0);787980//--------------------------------81// Re-initialize with a given seed82//--------------------------------8384void init (unsigned long int seed);858687//----------------------------------------------------------88// Get the next value in the sequence (range: [false, true])89//----------------------------------------------------------9091bool nextb ();929394//---------------------------------------------------------------95// Get the next value in the sequence (range: [0 ... 0xffffffff])96//---------------------------------------------------------------9798unsigned long int nexti ();99100101//------------------------------------------------------102// Get the next value in the sequence (range: [0 ... 1[)103//------------------------------------------------------104105float nextf ();106107108//-------------------------------------------------------------------109// Get the next value in the sequence (range [rangeMin ... rangeMax[)110//-------------------------------------------------------------------111112float nextf (float rangeMin, float rangeMax);113114115private:116117void next ();118119unsigned long int _state;120};121122123//--------------------------------------------------------124// Random-number generator based on the C Standard Library125// functions erand48(), nrand48() & company; generates a126// uniformly distributed sequence.127//--------------------------------------------------------128129class Rand48130{131public:132133//------------134// Constructor135//------------136137Rand48 (unsigned long int seed = 0);138139140//--------------------------------141// Re-initialize with a given seed142//--------------------------------143144void init (unsigned long int seed);145146147//----------------------------------------------------------148// Get the next value in the sequence (range: [false, true])149//----------------------------------------------------------150151bool nextb ();152153154//---------------------------------------------------------------155// Get the next value in the sequence (range: [0 ... 0x7fffffff])156//---------------------------------------------------------------157158long int nexti ();159160161//------------------------------------------------------162// Get the next value in the sequence (range: [0 ... 1[)163//------------------------------------------------------164165double nextf ();166167168//-------------------------------------------------------------------169// Get the next value in the sequence (range [rangeMin ... rangeMax[)170//-------------------------------------------------------------------171172double nextf (double rangeMin, double rangeMax);173174175private:176177unsigned short int _state[3];178};179180181//------------------------------------------------------------182// Return random points uniformly distributed in a sphere with183// radius 1 around the origin (distance from origin <= 1).184//------------------------------------------------------------185186template <class Vec, class Rand>187Vec188solidSphereRand (Rand &rand);189190191//-------------------------------------------------------------192// Return random points uniformly distributed on the surface of193// a sphere with radius 1 around the origin.194//-------------------------------------------------------------195196template <class Vec, class Rand>197Vec198hollowSphereRand (Rand &rand);199200201//-----------------------------------------------202// Return random numbers with a normal (Gaussian)203// distribution with zero mean and unit variance.204//-----------------------------------------------205206template <class Rand>207float208gaussRand (Rand &rand);209210211//----------------------------------------------------212// Return random points whose distance from the origin213// has a normal (Gaussian) distribution with zero mean214// and unit variance.215//----------------------------------------------------216217template <class Vec, class Rand>218Vec219gaussSphereRand (Rand &rand);220221222//---------------------------------223// erand48(), nrand48() and friends224//---------------------------------225226double erand48 (unsigned short state[3]);227double drand48 ();228long int nrand48 (unsigned short state[3]);229long int lrand48 ();230void srand48 (long int seed);231232233//---------------234// Implementation235//---------------236237238inline void239Rand32::init (unsigned long int seed)240{241_state = (seed * 0xa5a573a5L) ^ 0x5a5a5a5aL;242}243244245inline246Rand32::Rand32 (unsigned long int seed)247{248init (seed);249}250251252inline void253Rand32::next ()254{255_state = 1664525L * _state + 1013904223L;256}257258259inline bool260Rand32::nextb ()261{262next ();263// Return the 31st (most significant) bit, by and-ing with 2 ^ 31.264return !!(_state & 2147483648UL);265}266267268inline unsigned long int269Rand32::nexti ()270{271next ();272return _state & 0xffffffff;273}274275276inline float277Rand32::nextf (float rangeMin, float rangeMax)278{279float f = nextf();280return rangeMin * (1 - f) + rangeMax * f;281}282283284inline void285Rand48::init (unsigned long int seed)286{287seed = (seed * 0xa5a573a5L) ^ 0x5a5a5a5aL;288289_state[0] = (unsigned short int) (seed & 0xFFFF);290_state[1] = (unsigned short int) ((seed >> 16) & 0xFFFF);291_state[2] = (unsigned short int) (seed & 0xFFFF);292}293294295inline296Rand48::Rand48 (unsigned long int seed)297{298init (seed);299}300301302inline bool303Rand48::nextb ()304{305return Imath::nrand48 (_state) & 1;306}307308309inline long int310Rand48::nexti ()311{312return Imath::nrand48 (_state);313}314315316inline double317Rand48::nextf ()318{319return Imath::erand48 (_state);320}321322323inline double324Rand48::nextf (double rangeMin, double rangeMax)325{326double f = nextf();327return rangeMin * (1 - f) + rangeMax * f;328}329330331template <class Vec, class Rand>332Vec333solidSphereRand (Rand &rand)334{335Vec v;336337do338{339for (unsigned int i = 0; i < Vec::dimensions(); i++)340v[i] = (typename Vec::BaseType) rand.nextf (-1, 1);341}342while (v.length2() > 1);343344return v;345}346347348template <class Vec, class Rand>349Vec350hollowSphereRand (Rand &rand)351{352Vec v;353typename Vec::BaseType length;354355do356{357for (unsigned int i = 0; i < Vec::dimensions(); i++)358v[i] = (typename Vec::BaseType) rand.nextf (-1, 1);359360length = v.length();361}362while (length > 1 || length == 0);363364return v / length;365}366367368template <class Rand>369float370gaussRand (Rand &rand)371{372float x; // Note: to avoid numerical problems with very small373float y; // numbers, we make these variables singe-precision374float length2; // floats, but later we call the double-precision log()375// and sqrt() functions instead of logf() and sqrtf().376do377{378x = float (rand.nextf (-1, 1));379y = float (rand.nextf (-1, 1));380length2 = x * x + y * y;381}382while (length2 >= 1 || length2 == 0);383384return x * sqrt (-2 * log (double (length2)) / length2);385}386387388template <class Vec, class Rand>389Vec390gaussSphereRand (Rand &rand)391{392return hollowSphereRand <Vec> (rand) * gaussRand (rand);393}394395} // namespace Imath396397#endif398399400