blob: 792db33a6bc64882010d78c48f80ab0ba7535ed2 [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:16 +00001//===----------------------------------------------------------------------===//
2//
Howard Hinnant5b08a8a2010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnant3e519522010-05-11 19:42:16 +00004//
Howard Hinnant412dbeb2010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnant3e519522010-05-11 19:42:16 +00007//
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
20template <class UIntType, UIntType Min, UIntType Max>
21class rand1
22{
23public:
24 // types
25 typedef UIntType result_type;
26
27private:
28 result_type x_;
29
30 static_assert(Min < Max, "rand1 invalid parameters");
31public:
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
59void
60test1()
61{
62 typedef std::knuth_b E;
63
64 E e;
65 assert(e() == 152607844u);
66}
67
68void
69test2()
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
78void
79test3()
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
88int main()
89{
90 test1();
91 test2();
92 test3();
93}