blitz Version 0.10
|
00001 // -*- C++ -*- 00002 /*************************************************************************** 00003 * blitz/rand-dunif.h Discrete uniform generator 00004 * 00005 * $Id: rand-dunif.h,v 1.5 2011/03/25 22:41:16 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_RAND_DUNIF_H 00033 #define BZ_RAND_DUNIF_H 00034 00035 #ifndef BZ_RANDOM_H 00036 #include <blitz/random.h> 00037 #endif 00038 00039 #ifndef BZ_RAND_UNIFORM_H 00040 #include <blitz/rand-uniform.h> 00041 #endif 00042 00043 #include <math.h> 00044 00045 BZ_NAMESPACE(blitz) 00046 00047 template<typename P_uniform BZ_TEMPLATE_DEFAULT(Uniform)> 00048 class DiscreteUniform { 00049 00050 public: 00051 typedef int T_numtype; 00052 typedef P_uniform T_uniform; 00053 00054 DiscreteUniform(int low, int high, double=0) 00055 : low_(low), range_(high-low+1) 00056 { 00057 } 00058 00059 void randomize() 00060 { 00061 uniform_.randomize(); 00062 } 00063 00064 int random() 00065 { 00066 return int(uniform_.random() * range_ + low_); 00067 } 00068 00069 private: 00070 int low_, range_; 00071 T_uniform uniform_; 00072 }; 00073 00074 BZ_NAMESPACE_END 00075 00076 #endif // BZ_RAND_DUNIF_H 00077