1 | // -*- C++ -*- |
---|
2 | /*************************************************************************** |
---|
3 | * random/discrete-uniform.h Discrete uniform IRNG wrapper class |
---|
4 | * |
---|
5 | * $Id$ |
---|
6 | * |
---|
7 | * Copyright (C) 1997-2011 Todd Veldhuizen <tveldhui@acm.org> |
---|
8 | * |
---|
9 | * This file is a part of Blitz. |
---|
10 | * |
---|
11 | * Blitz is free software: you can redistribute it and/or modify |
---|
12 | * it under the terms of the GNU Lesser General Public License |
---|
13 | * as published by the Free Software Foundation, either version 3 |
---|
14 | * of the License, or (at your option) any later version. |
---|
15 | * |
---|
16 | * Blitz is distributed in the hope that it will be useful, |
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
19 | * GNU Lesser General Public License for more details. |
---|
20 | * |
---|
21 | * You should have received a copy of the GNU Lesser General Public |
---|
22 | * License along with Blitz. If not, see <http://www.gnu.org/licenses/>. |
---|
23 | * |
---|
24 | * Suggestions: blitz-devel@lists.sourceforge.net |
---|
25 | * Bugs: blitz-support@lists.sourceforge.net |
---|
26 | * |
---|
27 | * For more information, please see the Blitz++ Home Page: |
---|
28 | * https://sourceforge.net/projects/blitz/ |
---|
29 | * |
---|
30 | ***************************************************************************/ |
---|
31 | |
---|
32 | #ifndef BZ_RANDOM_DISCRETE_UNIFORM_H |
---|
33 | #define BZ_RANDOM_DISCRETE_UNIFORM_H |
---|
34 | |
---|
35 | #include <random/default.h> |
---|
36 | |
---|
37 | BZ_NAMESPACE(ranlib) |
---|
38 | |
---|
39 | template<typename T = unsigned int, typename IRNG = defaultIRNG, |
---|
40 | typename stateTag = defaultState> |
---|
41 | class DiscreteUniform : public IRNGWrapper<IRNG,stateTag> |
---|
42 | { |
---|
43 | public: |
---|
44 | typedef T T_numtype; |
---|
45 | |
---|
46 | DiscreteUniform(T n) |
---|
47 | { |
---|
48 | BZPRECONDITION(n < 4294967295U); |
---|
49 | n_ = n; |
---|
50 | } |
---|
51 | |
---|
52 | DiscreteUniform(T n, unsigned int i) : |
---|
53 | IRNGWrapper<IRNG,stateTag>::IRNGWrapper(i) |
---|
54 | { |
---|
55 | BZPRECONDITION(n < 4294967295U); |
---|
56 | n_ = n; |
---|
57 | } |
---|
58 | |
---|
59 | T random() |
---|
60 | { |
---|
61 | return this->irng_.random() % n_; |
---|
62 | } |
---|
63 | |
---|
64 | private: |
---|
65 | T n_; |
---|
66 | }; |
---|
67 | |
---|
68 | BZ_NAMESPACE_END |
---|
69 | |
---|
70 | #endif // BZ_RANDOM_DISCRETE_UNIFORM_H |
---|