blob: bc96a12cd29ccb692ce2a25f858ecf24ee1f55b3 [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
Austin Schuhb4691e92020-12-31 12:37:18 -080024#include "absl/base/config.h"
25
Austin Schuh36244a12019-09-21 17:52:38 -070026namespace absl {
Austin Schuhb4691e92020-12-31 12:37:18 -080027ABSL_NAMESPACE_BEGIN
Austin Schuh36244a12019-09-21 17:52:38 -070028namespace random_internal {
29
30// `sequence_urbg` is a simple random number generator which meets the
31// requirements of [rand.req.urbg], and is solely for testing absl
32// distributions.
33class sequence_urbg {
34 public:
35 using result_type = uint64_t;
36
37 static constexpr result_type(min)() {
38 return (std::numeric_limits<result_type>::min)();
39 }
40 static constexpr result_type(max)() {
41 return (std::numeric_limits<result_type>::max)();
42 }
43
44 sequence_urbg(std::initializer_list<result_type> data) : i_(0), data_(data) {}
45 void reset() { i_ = 0; }
46
47 result_type operator()() { return data_[i_++ % data_.size()]; }
48
49 size_t invocations() const { return i_; }
50
51 private:
52 size_t i_;
53 std::vector<result_type> data_;
54};
55
56} // namespace random_internal
Austin Schuhb4691e92020-12-31 12:37:18 -080057ABSL_NAMESPACE_END
Austin Schuh36244a12019-09-21 17:52:38 -070058} // namespace absl
59
60#endif // ABSL_RANDOM_INTERNAL_SEQUENCE_URBG_H_