examples_test.cc 3.0 KB

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