pl-nk v0.4.5
Plonk|Plink|Plank are a set of cross-platform C/C++ frameworks for audio software development
plonk_RNG.h
00001 /*
00002  -------------------------------------------------------------------------------
00003  This file is part of the Plink, Plonk, Plank libraries
00004   by Martin Robinson
00005  
00006  http://code.google.com/p/pl-nk/
00007  
00008  Copyright University of the West of England, Bristol 2011-14
00009  All rights reserved.
00010  
00011  Redistribution and use in source and binary forms, with or without
00012  modification, are permitted provided that the following conditions are met:
00013  
00014  * Redistributions of source code must retain the above copyright
00015    notice, this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright
00017    notice, this list of conditions and the following disclaimer in the
00018    documentation and/or other materials provided with the distribution.
00019  * Neither the name of University of the West of England, Bristol nor 
00020    the names of its contributors may be used to endorse or promote products
00021    derived from this software without specific prior written permission.
00022  
00023  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00024  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00025  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
00026  DISCLAIMED. IN NO EVENT SHALL UNIVERSITY OF THE WEST OF ENGLAND, BRISTOL BE 
00027  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
00028  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
00029  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
00030  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00031  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
00032  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
00033  
00034  This software makes use of third party libraries. For more information see:
00035  doc/license.txt included in the distribution.
00036  -------------------------------------------------------------------------------
00037  */
00038 
00039 #ifndef PLONK_RNG_H
00040 #define PLONK_RNG_H
00041 
00042 #include "../core/plonk_CoreForwardDeclarations.h"
00043 #include "../containers/plonk_ContainerForwardDeclarations.h"
00044 
00045 #include "../core/plonk_SmartPointer.h"
00046 #include "../core/plonk_WeakPointer.h"
00047 
00048 
00049 class RNGInternal : public SmartPointer
00050 {
00051 public:
00052     RNGInternal() throw();
00053     ~RNGInternal();
00054     
00055     friend class RNG;
00056     
00057 private:
00058     inline PlankRNGRef getRNGRef() { return &rng; }
00059 
00060     PlankRNG rng;
00061 };
00062 
00065 class RNG : SmartPointerContainer<RNGInternal>
00066 {
00067 public:
00068     typedef RNGInternal                         Internal;
00069     typedef SmartPointerContainer<RNGInternal>  Base;
00070     
00071     RNG() throw();
00072     RNG (RNG const& copy) throw();
00073     RNG& operator= (RNG const& other) throw();
00074 
00076     static RNG& global() throw();
00077     
00080     static RNG& audio() throw();
00081     
00083     void seed (const unsigned int value) throw();
00084     
00086     unsigned int uniformInt() throw();
00087     
00089     unsigned int uniform (const unsigned int max) throw();
00090     
00092     unsigned int uniform (const unsigned int min, const unsigned int max) throw();
00093     
00095     int uniform (const int max) throw();
00096     
00098     int uniform (const int min, const int max) throw();
00099     
00101     float uniformFloat() throw();
00102     
00104     float uniform (const float max) throw();
00105     
00107     float uniform (const float min, const float max) throw();
00108     
00110     double uniformDouble() throw();
00111     
00113     double uniform (const double max) throw();
00114     
00116     double uniform (const double min, const double max) throw();
00117     
00119     float exponential (const float min, const float max) throw();
00120     
00122     double exponential (const double min, const double max) throw();     
00123     
00124     PLONK_OBJECTARROWOPERATOR(RNG);
00125 };
00126 
00127 //------------------------------------------------------------------------------
00128 
00129 inline double rand (const double scale) throw()
00130 {
00131         return RNG::global().uniform (scale);
00132 }
00133 
00134 inline double rand (const double min, const double max) throw()
00135 {
00136         return RNG::global().uniform (min, max);
00137 }
00138 
00139 inline double rand2 (const double scale) throw()
00140 {
00141         return RNG::global().uniform (scale);
00142 }
00143 
00144 inline double exprand (const double min, const double max) throw()
00145 {
00146         return RNG::global().exponential (min, max);
00147 }
00148 
00149 inline float rand (const float scale) throw()
00150 {
00151         return RNG::global().uniform (scale);
00152 }
00153 
00154 inline float rand (const float min, const float max) throw()
00155 {
00156         return RNG::global().uniform (min, max);
00157 }
00158 
00159 inline float rand2 (const float scale) throw()
00160 {
00161         return RNG::global().uniform (-scale, scale);
00162 }
00163 
00164 inline float exprand (const float min, const float max) throw()
00165 {
00166         return RNG::global().exponential (min, max);
00167 }
00168 
00169 inline int rand (const int scale) throw()
00170 {
00171         return RNG::global().uniform (scale);
00172 }
00173 
00174 inline int rand (const int min, const int max) throw()
00175 {
00176         return RNG::global().uniform (min, max);
00177 }
00178 
00179 inline int rand2 (const int scale) throw()
00180 {
00181         return RNG::global().uniform (-scale, scale);
00182 }
00183 
00184 inline Int24 rand (const Int24 scale) throw()
00185 {
00186         return RNG::global().uniform (int (scale));
00187 }
00188 
00189 inline Int24 rand (const Int24 min, const Int24 max) throw()
00190 {
00191         return RNG::global().uniform (int (min), int (max));
00192 }
00193 
00194 inline Int24 rand2 (const Int24 scale) throw()
00195 {
00196     const int intScale (scale);
00197         return RNG::global().uniform (-intScale, intScale);
00198 }
00199 
00200 inline double exprand (const int min, const int max) throw()
00201 {
00202         return RNG::global().exponential (double (min), double (max));
00203 }
00204 
00205 
00206 #endif // PLONK_RNG_H
 All Classes Functions Typedefs Enumerations Enumerator Properties