Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Howard Hinnant | 5b08a8a | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | 412dbeb | 2010-11-16 22:09:02 +0000 | [diff] [blame^] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // <random> |
| 11 | |
| 12 | // template<class Engine, size_t k> |
| 13 | // class shuffle_order_engine |
| 14 | |
| 15 | // result_type operator()(); |
| 16 | |
| 17 | #include <random> |
| 18 | #include <cassert> |
| 19 | |
| 20 | template <class UIntType, UIntType Min, UIntType Max> |
| 21 | class rand1 |
| 22 | { |
| 23 | public: |
| 24 | // types |
| 25 | typedef UIntType result_type; |
| 26 | |
| 27 | private: |
| 28 | result_type x_; |
| 29 | |
| 30 | static_assert(Min < Max, "rand1 invalid parameters"); |
| 31 | public: |
| 32 | |
| 33 | // Temporary work around for lack of constexpr |
| 34 | static const result_type _Min = Min; |
| 35 | static const result_type _Max = Max; |
| 36 | |
| 37 | static const/*expr*/ result_type min() {return Min;} |
| 38 | static const/*expr*/ result_type max() {return Max;} |
| 39 | |
| 40 | explicit rand1(result_type sd = Min) : x_(sd) |
| 41 | { |
| 42 | if (x_ < Min) |
| 43 | x_ = Min; |
| 44 | if (x_ > Max) |
| 45 | x_ = Max; |
| 46 | } |
| 47 | |
| 48 | result_type operator()() |
| 49 | { |
| 50 | result_type r = x_; |
| 51 | if (x_ < Max) |
| 52 | ++x_; |
| 53 | else |
| 54 | x_ = Min; |
| 55 | return r; |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | void |
| 60 | test1() |
| 61 | { |
| 62 | typedef std::knuth_b E; |
| 63 | |
| 64 | E e; |
| 65 | assert(e() == 152607844u); |
| 66 | } |
| 67 | |
| 68 | void |
| 69 | test2() |
| 70 | { |
| 71 | typedef rand1<unsigned long long, 0, 0xFFFFFFFFFFFFFFFFull> E0; |
| 72 | typedef std::shuffle_order_engine<E0, 101> E; |
| 73 | E e; |
| 74 | e.discard(400); |
| 75 | assert(e() == 501); |
| 76 | } |
| 77 | |
| 78 | void |
| 79 | test3() |
| 80 | { |
| 81 | typedef rand1<unsigned long long, 0, 0xFFFFFFFFFFFFFFFFull> E0; |
| 82 | typedef std::shuffle_order_engine<E0, 100> E; |
| 83 | E e; |
| 84 | e.discard(400); |
| 85 | assert(e() == 500); |
| 86 | } |
| 87 | |
| 88 | int main() |
| 89 | { |
| 90 | test1(); |
| 91 | test2(); |
| 92 | test3(); |
| 93 | } |