1 | /* Copyright 2015 The TensorFlow Authors. All Rights Reserved. |
---|---|
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 | http://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 | |
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 | |
23 | namespace tsl { |
24 | namespace random { |
25 | |
26 | namespace { |
27 | std::mt19937_64* InitRngWithRandomSeed() { |
28 | std::random_device device("/dev/urandom"); |
29 | return new std::mt19937_64(device()); |
30 | } |
31 | std::mt19937_64 InitRngWithDefaultSeed() { return std::mt19937_64(); } |
32 | |
33 | } // anonymous namespace |
34 | |
35 | uint64 New64() { |
36 | static std::mt19937_64* rng = InitRngWithRandomSeed(); |
37 | static mutex mu(LINKER_INITIALIZED); |
38 | mutex_lock l(mu); |
39 | return (*rng)(); |
40 | } |
41 | |
42 | uint64 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 |