2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <folly/Random.h>
22 #include <unordered_set>
25 #include <glog/logging.h>
27 #include <folly/portability/GTest.h>
29 using namespace folly;
31 TEST(Random, StateSize) {
32 using namespace folly::detail;
34 // uint_fast32_t is uint64_t on x86_64, w00t
36 sizeof(uint_fast32_t) / 4 + 3, StateSizeT<std::minstd_rand0>::value);
37 EXPECT_EQ(624, StateSizeT<std::mt19937>::value);
38 #if FOLLY_HAVE_EXTRANDOM_SFMT19937
39 EXPECT_EQ(624, StateSizeT<__gnu_cxx::sfmt19937>::value);
41 EXPECT_EQ(24, StateSizeT<std::ranlux24_base>::value);
44 TEST(Random, Simple) {
45 uint32_t prev = 0, seed = 0;
46 for (int i = 0; i < 1024; ++i) {
47 EXPECT_NE(seed = randomNumberSeed(), prev);
52 TEST(Random, FixedSeed) {
55 typedef uint32_t result_type;
56 result_type operator()() {
57 return 4; // chosen by fair dice roll.
58 // guaranteed to be random.
60 static constexpr result_type min() {
61 return std::numeric_limits<result_type>::min();
63 static constexpr result_type max() {
64 return std::numeric_limits<result_type>::max();
71 // Pick a constant random number...
72 auto value = Random::rand32(10, gen);
74 // Loop to make sure it really is constant.
75 for (int i = 0; i < 1024; ++i) {
76 auto result = Random::rand32(10, gen);
77 EXPECT_EQ(value, result);
81 TEST(Random, MultiThreaded) {
83 std::vector<uint32_t> seeds(n);
84 std::vector<std::thread> threads;
85 for (int i = 0; i < n; ++i) {
86 threads.push_back(std::thread([i, &seeds] {
87 seeds[i] = randomNumberSeed();
90 for (auto& t : threads) {
93 std::sort(seeds.begin(), seeds.end());
94 for (int i = 0; i < n-1; ++i) {
95 EXPECT_LT(seeds[i], seeds[i+1]);
99 TEST(Random, sanity) {
101 EXPECT_EQ(folly::Random::rand32(0), 0);
102 EXPECT_EQ(folly::Random::rand32(12, 12), 0);
103 EXPECT_EQ(folly::Random::rand64(0), 0);
104 EXPECT_EQ(folly::Random::rand64(12, 12), 0);
106 // 32-bit repeatability, uniqueness
107 constexpr int kTestSize = 1000;
109 std::vector<uint32_t> vals;
110 folly::Random::DefaultGenerator rng;
111 rng.seed(0xdeadbeef);
112 for (int i = 0; i < kTestSize; ++i) {
113 vals.push_back(folly::Random::rand32(rng));
115 rng.seed(0xdeadbeef);
116 for (int i = 0; i < kTestSize; ++i) {
117 EXPECT_EQ(vals[i], folly::Random::rand32(rng));
121 std::unordered_set<uint32_t>(vals.begin(), vals.end()).size());
124 // 64-bit repeatability, uniqueness
126 std::vector<uint64_t> vals;
127 folly::Random::DefaultGenerator rng;
128 rng.seed(0xdeadbeef);
129 for (int i = 0; i < kTestSize; ++i) {
130 vals.push_back(folly::Random::rand64(rng));
132 rng.seed(0xdeadbeef);
133 for (int i = 0; i < kTestSize; ++i) {
134 EXPECT_EQ(vals[i], folly::Random::rand64(rng));
138 std::unordered_set<uint64_t>(vals.begin(), vals.end()).size());