1/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#include "tensorflow/tsl/platform/random.h"
17
18#include <random>
19
20#include "tensorflow/tsl/platform/mutex.h"
21#include "tensorflow/tsl/platform/types.h"
22
23namespace tsl {
24namespace random {
25
26namespace {
27std::mt19937_64* InitRngWithRandomSeed() {
28 std::random_device device("/dev/urandom");
29 return new std::mt19937_64(device());
30}
31std::mt19937_64 InitRngWithDefaultSeed() { return std::mt19937_64(); }
32
33} // anonymous namespace
34
35uint64 New64() {
36 static std::mt19937_64* rng = InitRngWithRandomSeed();
37 static mutex mu(LINKER_INITIALIZED);
38 mutex_lock l(mu);
39 return (*rng)();
40}
41
42uint64 New64DefaultSeed() {
43 static std::mt19937_64 rng = InitRngWithDefaultSeed();
44 static mutex mu(LINKER_INITIALIZED);
45 mutex_lock l(mu);
46 return rng();
47}
48
49} // namespace random
50} // namespace tsl
51