Home | History | Annotate | Download | only in random
      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/core/lib/random/random.h"
     17 
     18 #include <random>
     19 #include "tensorflow/core/platform/mutex.h"
     20 #include "tensorflow/core/platform/types.h"
     21 
     22 namespace tensorflow {
     23 namespace random {
     24 
     25 namespace {
     26 std::mt19937_64* InitRngWithRandomSeed() {
     27   std::random_device device("/dev/urandom");
     28   return new std::mt19937_64(device());
     29 }
     30 std::mt19937_64 InitRngWithDefaultSeed() { return std::mt19937_64(); }
     31 
     32 }  // anonymous namespace
     33 
     34 uint64 New64() {
     35   static std::mt19937_64* rng = InitRngWithRandomSeed();
     36   static mutex mu(LINKER_INITIALIZED);
     37   mutex_lock l(mu);
     38   return (*rng)();
     39 }
     40 
     41 uint64 New64DefaultSeed() {
     42   static std::mt19937_64 rng = InitRngWithDefaultSeed();
     43   static mutex mu(LINKER_INITIALIZED);
     44   mutex_lock l(mu);
     45   return rng();
     46 }
     47 
     48 }  // namespace random
     49 }  // namespace tensorflow
     50