2 * Copyright 2014 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"
25 #if __GNUC_PREREQ(4, 8)
33 std::atomic<uint32_t> seedInput(0);
36 uint32_t randomNumberSeed() {
38 gettimeofday(&tv, nullptr);
39 const uint32_t kPrime0 = 51551;
40 const uint32_t kPrime1 = 61631;
41 const uint32_t kPrime2 = 64997;
42 const uint32_t kPrime3 = 111857;
43 return kPrime0 * (seedInput++)
44 + kPrime1 * static_cast<uint32_t>(getpid())
45 + kPrime2 * static_cast<uint32_t>(tv.tv_sec)
46 + kPrime3 * static_cast<uint32_t>(tv.tv_usec);
50 folly::ThreadLocalPtr<ThreadLocalPRNG::LocalInstancePRNG>
51 ThreadLocalPRNG::localInstance;
53 class ThreadLocalPRNG::LocalInstancePRNG {
55 typedef __gnu_cxx::sfmt19937 RNG;
57 typedef std::mt19937 RNG;
60 static RNG makeRng() {
61 std::array<int, RNG::state_size> seed_data;
63 std::generate_n(seed_data.data(), seed_data.size(), std::ref(r));
64 std::seed_seq seq(std::begin(seed_data), std::end(seed_data));
69 LocalInstancePRNG() : rng(std::move(makeRng())) {}
74 ThreadLocalPRNG::LocalInstancePRNG* ThreadLocalPRNG::initLocal() {
75 auto ret = new LocalInstancePRNG;
76 localInstance.reset(ret);
80 uint32_t ThreadLocalPRNG::getImpl(LocalInstancePRNG* local) {