appkit  1.5.1
RoadNarrows Robotics Application Kit
Random.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: RoadNarrows Robotics Application Tool Kit
4 //
5 // Link: https://github.com/roadnarrows-robotics/rnr-sdk
6 //
7 // Library: librnr_appkit
8 //
9 // File: Random.h
10 //
11 /*! \file
12  *
13  * $LastChangedDate: 2015-11-09 17:38:34 -0700 (Mon, 09 Nov 2015) $
14  * $Rev: 4195 $
15  *
16  * \brief Random variable generator class interface.
17  *
18  * \author Daniel Packard (daniel@roadnarrows.com)
19  * \author Robin Knight (robin.knight@roadnarrows.com)
20  *
21  * \par Copyright
22  * \h_copy 2012-2017. RoadNarrows LLC.\n
23  * http://www.roadnarrows.com\n
24  * All Rights Reserved
25  */
26 /*
27  * @EulaBegin@
28  *
29  * Permission is hereby granted, without written agreement and without
30  * license or royalty fees, to use, copy, modify, and distribute this
31  * software and its documentation for any purpose, provided that
32  * (1) The above copyright notice and the following two paragraphs
33  * appear in all copies of the source code and (2) redistributions
34  * including binaries reproduces these notices in the supporting
35  * documentation. Substantial modifications to this software may be
36  * copyrighted by their authors and need not follow the licensing terms
37  * described here, provided that the new terms are clearly indicated in
38  * all files where they apply.
39  *
40  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
41  * OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
42  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
43  * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
44  * EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
45  * THE POSSIBILITY OF SUCH DAMAGE.
46  *
47  * THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
48  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
49  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
50  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
51  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
52  *
53  * @EulaEnd@
54  */
55 ////////////////////////////////////////////////////////////////////////////////
56 
57 #ifndef _RNR_RANDOM_H
58 #define _RNR_RANDOM_H
59 
60 #include <stdlib.h>
61 #include <limits.h>
62 
63 #include "rnr/rnrconfig.h"
64 
65 /*!
66  * \brief RoadNarrows Robotics
67  */
68 namespace rnr
69 {
70  //----------------------------------------------------------------------------
71  // Random Class
72  //----------------------------------------------------------------------------
73 
74  /*!
75  * \ingroup rnr_appkit
76  *
77  * \brief Random variable generators class.
78  */
79  class Random
80  {
81  public:
82  static const ulong_t AUTO_SEED = 0;
83 
84  /*!
85  * \brief Default initialization constructor.
86  *
87  * \param luSeed Random generator seed.
88  */
89  Random(ulong_t luSeed = AUTO_SEED);
90 
91  /*!
92  * \brief Destructor.
93  */
94  virtual ~Random() { }
95 
96  /*!
97  * \brief Generates a random integer uniformally distrubuted
98  * between [-2^31, 2^32-1].
99  *
100  * \return Random value.
101  */
102  int randint()
103  {
104  long lRandVal;
105 
106  mrand48_r(&m_randState, &lRandVal);
107 
108  return (int)lRandVal;
109  }
110 
111  /*!
112  * \brief Generates a random integer uniformally distrubuted
113  * between [nMin, nMax].
114  *
115  * \param nMin Minimum value.
116  * \param nMax Maximum value.
117  *
118  * \return Random value.
119  */
120  int randrange(int nMin=0, int nMax=INT_MAX);
121 
122  /*!
123  * \brief Generates a random 64-bit integer uniformally distrubuted
124  * between [nMin, nMax].
125  *
126  * \param nMin Minimum value.
127  * \param nMax Maximum value.
128  *
129  * \return Random value.
130  */
131  s64_t randrange(s64_t nMin=0, s64_t nMax=LLONG_MAX);
132 
133  /*!
134  * \brief Generates a random float uniformally distrubuted
135  * between [fMin, fMax].
136  *
137  * \param fMin Minimum value.
138  * \param fMax Maximum value.
139  *
140  * \return Random value.
141  */
142  float uniform(float fMin=0.0, float fMax=1.0);
143 
144  protected:
145  struct drand48_data m_randState; ///< random generator opaque state.
146  };
147 }
148 
149 #endif // _RNR_RANDOM_H
int randint()
Generates a random integer uniformally distrubuted between [-2^31, 2^32-1].
Definition: Random.h:102
float uniform(float fMin=0.0, float fMax=1.0)
Generates a random float uniformally distrubuted between [fMin, fMax].
Definition: Random.cxx:106
struct drand48_data m_randState
random generator opaque state.
Definition: Random.h:145
int randrange(int nMin=0, int nMax=INT_MAX)
Generates a random integer uniformally distrubuted between [nMin, nMax].
Definition: Random.cxx:88
Random variable generators class.
Definition: Random.h:79
virtual ~Random()
Destructor.
Definition: Random.h:94
Random(ulong_t luSeed=AUTO_SEED)
Default initialization constructor.
Definition: Random.cxx:72
RoadNarrows Robotics.
Definition: Camera.h:74