seed.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2013-2014, Willow Garage, Inc.
5  * Copyright (c) 2014-2016, Open Source Robotics Foundation
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of Open Source Robotics Foundation nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
38 #include "fcl/math/detail/seed.h"
39 
40 #include <chrono>
41 #include <mutex>
42 #include <random>
43 
44 namespace fcl
45 {
46 namespace detail
47 {
48 
49 //==============================================================================
51 {
53 }
54 
55 //==============================================================================
56 uint_fast32_t Seed::getUserSetSeed()
57 {
58  return getInstance().userSetSeed;
59 }
60 
61 //==============================================================================
62 void Seed::setUserSetSeed(uint_fast32_t seed)
63 {
64  getInstance().userSetSeed = seed;
65 }
66 
67 //==============================================================================
68 uint_fast32_t Seed::getFirstSeed()
69 {
70  // Compute the first seed to be used; this function should be called only once
71  static std::mutex fsLock;
72  std::unique_lock<std::mutex> slock(fsLock);
73 
75  return getInstance().firstSeedValue;
76 
77  if (getInstance().userSetSeed != 0)
78  {
80  }
81  else
82  {
84  = static_cast<std::uint_fast32_t>(
85  std::chrono::duration_cast<std::chrono::microseconds>(
87  - std::chrono::system_clock::time_point()).count());
88  }
89 
91 
92  return getInstance().firstSeedValue;
93 }
94 
95 //==============================================================================
96 uint_fast32_t Seed::getNextSeed()
97 {
98  static std::mutex rngMutex;
99  std::unique_lock<std::mutex> slock(rngMutex);
100  static std::ranlux24_base sGen;
101  static std::uniform_int_distribution<> sDist(1, 1000000000);
102 
103  return sDist(sGen);
104 }
105 
106 //==============================================================================
107 Seed::Seed() : userSetSeed(0), firstSeedGenerated(false), firstSeedValue(0)
108 {
109  // Do nothing
110 }
111 
112 //==============================================================================
114 {
115  static Seed seed;
116 
117  return seed;
118 }
119 
120 } // namespace detail
121 } // namespace fcl
fcl::detail::Seed::setUserSetSeed
static void setUserSetSeed(std::uint_fast32_t seed)
Definition: seed.cpp:62
fcl::detail::Seed::isFirstSeedGenerated
static bool isFirstSeedGenerated()
Definition: seed.cpp:50
fcl::detail::Seed::getUserSetSeed
static std::uint_fast32_t getUserSetSeed()
Definition: seed.cpp:56
fcl::detail::Seed::getNextSeed
static std::uint_fast32_t getNextSeed()
Definition: seed.cpp:96
fcl::detail::Seed::userSetSeed
std::uint_fast32_t userSetSeed
The seed the user asked for (cannot be 0)
Definition: seed.h:73
fcl::detail::Seed::Seed
Seed()
Definition: seed.cpp:107
fcl::detail::Seed::getFirstSeed
static std::uint_fast32_t getFirstSeed()
Definition: seed.cpp:68
fcl::detail::Seed
Definition: seed.h:49
fcl::detail::Seed::firstSeedValue
std::uint_fast32_t firstSeedValue
The value of the first seed.
Definition: seed.h:79
seed.h
fcl::time::now
FCL_EXPORT point now(void)
Get the current time point.
Definition: time.cpp:47
fcl::detail::Seed::getInstance
static Seed & getInstance()
Definition: seed.cpp:113
fcl
Main namespace.
Definition: broadphase_bruteforce-inl.h:45
fcl::detail::Seed::firstSeedGenerated
bool firstSeedGenerated
Flag indicating whether the first seed has already been generated or not.
Definition: seed.h:76


fcl
Author(s):
autogenerated on Tue Dec 5 2023 03:40:48