1 // 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 #include <cinttypes>
16 #include <random>
17 #include <sstream>
18 #include <vector>
19
20 #include "gtest/gtest.h"
21 #include "absl/random/random.h"
22
23 template <typename T>
Use(T)24 void Use(T) {}
25
TEST(Examples,Basic)26 TEST(Examples, Basic) {
27 absl::BitGen gen;
28 std::vector<int> objs = {10, 20, 30, 40, 50};
29
30 // Choose an element from a set.
31 auto elem = objs[absl::Uniform(gen, 0u, objs.size())];
32 Use(elem);
33
34 // Generate a uniform value between 1 and 6.
35 auto dice_roll = absl::Uniform<int>(absl::IntervalClosedClosed, gen, 1, 6);
36 Use(dice_roll);
37
38 // Generate a random byte.
39 auto byte = absl::Uniform<uint8_t>(gen);
40 Use(byte);
41
42 // Generate a fractional value from [0f, 1f).
43 auto fraction = absl::Uniform<float>(gen, 0, 1);
44 Use(fraction);
45
46 // Toss a fair coin; 50/50 probability.
47 bool coin_toss = absl::Bernoulli(gen, 0.5);
48 Use(coin_toss);
49
50 // Select a file size between 1k and 10MB, biased towards smaller file sizes.
51 auto file_size = absl::LogUniform<size_t>(gen, 1000, 10 * 1000 * 1000);
52 Use(file_size);
53
54 // Randomize (shuffle) a collection.
55 std::shuffle(std::begin(objs), std::end(objs), gen);
56 }
57
TEST(Examples,CreateingCorrelatedVariateSequences)58 TEST(Examples, CreateingCorrelatedVariateSequences) {
59 // Unexpected PRNG correlation is often a source of bugs,
60 // so when using absl::BitGen it must be an intentional choice.
61 // NOTE: All of these only exhibit process-level stability.
62
63 // Create a correlated sequence from system entropy.
64 {
65 auto my_seed = absl::MakeSeedSeq();
66
67 absl::BitGen gen_1(my_seed);
68 absl::BitGen gen_2(my_seed); // Produces same variates as gen_1.
69
70 EXPECT_EQ(absl::Bernoulli(gen_1, 0.5), absl::Bernoulli(gen_2, 0.5));
71 EXPECT_EQ(absl::Uniform<uint32_t>(gen_1), absl::Uniform<uint32_t>(gen_2));
72 }
73
74 // Create a correlated sequence from an existing URBG.
75 {
76 absl::BitGen gen;
77
78 auto my_seed = absl::CreateSeedSeqFrom(&gen);
79 absl::BitGen gen_1(my_seed);
80 absl::BitGen gen_2(my_seed);
81
82 EXPECT_EQ(absl::Bernoulli(gen_1, 0.5), absl::Bernoulli(gen_2, 0.5));
83 EXPECT_EQ(absl::Uniform<uint32_t>(gen_1), absl::Uniform<uint32_t>(gen_2));
84 }
85
86 // An alternate construction which uses user-supplied data
87 // instead of a random seed.
88 {
89 const char kData[] = "A simple seed string";
90 std::seed_seq my_seed(std::begin(kData), std::end(kData));
91
92 absl::BitGen gen_1(my_seed);
93 absl::BitGen gen_2(my_seed);
94
95 EXPECT_EQ(absl::Bernoulli(gen_1, 0.5), absl::Bernoulli(gen_2, 0.5));
96 EXPECT_EQ(absl::Uniform<uint32_t>(gen_1), absl::Uniform<uint32_t>(gen_2));
97 }
98 }
99
100