blob: 9a9b57738da466d38f10d6499a8648fff456c950 [file] [log] [blame]
Austin Schuh36244a12019-09-21 17:52:38 -07001// Copyright 2017 The Abseil Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef ABSL_RANDOM_INTERNAL_SEQUENCE_URBG_H_
16#define ABSL_RANDOM_INTERNAL_SEQUENCE_URBG_H_
17
18#include <cstdint>
19#include <cstring>
20#include <limits>
21#include <type_traits>
22#include <vector>
23
24namespace absl {
25namespace random_internal {
26
27// `sequence_urbg` is a simple random number generator which meets the
28// requirements of [rand.req.urbg], and is solely for testing absl
29// distributions.
30class sequence_urbg {
31 public:
32 using result_type = uint64_t;
33
34 static constexpr result_type(min)() {
35 return (std::numeric_limits<result_type>::min)();
36 }
37 static constexpr result_type(max)() {
38 return (std::numeric_limits<result_type>::max)();
39 }
40
41 sequence_urbg(std::initializer_list<result_type> data) : i_(0), data_(data) {}
42 void reset() { i_ = 0; }
43
44 result_type operator()() { return data_[i_++ % data_.size()]; }
45
46 size_t invocations() const { return i_; }
47
48 private:
49 size_t i_;
50 std::vector<result_type> data_;
51};
52
53} // namespace random_internal
54} // namespace absl
55
56#endif // ABSL_RANDOM_INTERNAL_SEQUENCE_URBG_H_