blitz Version 0.10
random/discrete-uniform.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 /***************************************************************************
00003  * random/discrete-uniform.h       Discrete uniform IRNG wrapper class
00004  *
00005  * $Id: discrete-uniform.h,v 1.5 2011/03/25 22:41:17 julianc Exp $
00006  *
00007  * Copyright (C) 1997-2011 Todd Veldhuizen <tveldhui@acm.org>
00008  *
00009  * This file is a part of Blitz.
00010  *
00011  * Blitz is free software: you can redistribute it and/or modify 
00012  * it under the terms of the GNU Lesser General Public License
00013  * as published by the Free Software Foundation, either version 3
00014  * of the License, or (at your option) any later version.
00015  *
00016  * Blitz is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU Lesser General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU Lesser General Public 
00022  * License along with Blitz.  If not, see <http://www.gnu.org/licenses/>.
00023  * 
00024  * Suggestions:          blitz-devel@lists.sourceforge.net
00025  * Bugs:                 blitz-support@lists.sourceforge.net    
00026  *
00027  * For more information, please see the Blitz++ Home Page:
00028  *    https://sourceforge.net/projects/blitz/
00029  *
00030  ***************************************************************************/
00031 
00032 #ifndef BZ_RANDOM_DISCRETE_UNIFORM_H
00033 #define BZ_RANDOM_DISCRETE_UNIFORM_H
00034 
00035 #include <random/default.h>
00036 
00037 BZ_NAMESPACE(ranlib)
00038 
00039 template<typename T = unsigned int, typename IRNG = defaultIRNG,
00040     typename stateTag = defaultState>
00041 class DiscreteUniform : public IRNGWrapper<IRNG,stateTag>
00042 {
00043 public:
00044     typedef T T_numtype;
00045 
00046     DiscreteUniform(T n)
00047     {
00048         BZPRECONDITION(n < 4294967295U);
00049         n_ = n;
00050     }
00051 
00052   DiscreteUniform(T n, unsigned int i) :
00053     IRNGWrapper<IRNG,stateTag>::IRNGWrapper(i)
00054     {
00055         BZPRECONDITION(n < 4294967295U);
00056         n_ = n;
00057     }
00058 
00059     T random()
00060     {
00061         return this->irng_.random() % n_;
00062     }
00063 
00064 private:
00065     T n_;
00066 };
00067 
00068 BZ_NAMESPACE_END
00069 
00070 #endif // BZ_RANDOM_DISCRETE_UNIFORM_H
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines