appkit  1.5.1
RoadNarrows Robotics Application Kit
Random.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: RoadNarrows Robotics Application Tool Kit
4 //
5 // Library: librnr_appkit
6 //
7 // File: Random.cxx
8 //
9 //
10 /*! \file
11  *
12  * $LastChangedDate: 2015-11-09 11:56:44 -0700 (Mon, 09 Nov 2015) $
13  * $Rev: 4194 $
14  *
15  * \brief Random variable generator class implementation.
16  *
17  * \author Daniel Packard (daniel@roadnarrows.com)
18  * \author Robin Knight (robin.knight@roadnarrows.com)
19  *
20  * \par Copyright
21  * \h_copy 2012-2017. RoadNarrows LLC.\n
22  * http://www.roadnarrows.com\n
23  * All Rights Reserved
24  */
25 /*
26  * @EulaBegin@
27  *
28  * Permission is hereby granted, without written agreement and without
29  * license or royalty fees, to use, copy, modify, and distribute this
30  * software and its documentation for any purpose, provided that
31  * (1) The above copyright notice and the following two paragraphs
32  * appear in all copies of the source code and (2) redistributions
33  * including binaries reproduces these notices in the supporting
34  * documentation. Substantial modifications to this software may be
35  * copyrighted by their authors and need not follow the licensing terms
36  * described here, provided that the new terms are clearly indicated in
37  * all files where they apply.
38  *
39  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
40  * OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
41  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
42  * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
43  * EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
44  * THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  * THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
47  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
48  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
49  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
50  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
51  *
52  * @EulaEnd@
53  */
54 ////////////////////////////////////////////////////////////////////////////////
55 
56 #include <stdlib.h>
57 #include <time.h>
58 
59 #include "rnr/rnrconfig.h"
60 #include "rnr/log.h"
61 
62 #include "rnr/appkit/Random.h"
63 
64 
65 using namespace rnr;
66 
67 
68 // -----------------------------------------------------------------------------
69 // Random Class
70 // -----------------------------------------------------------------------------
71 
72 Random::Random(ulong_t luSeed)
73 {
74  ushort_t seed[3]; // 48-bits
75 
76  if( luSeed == AUTO_SEED )
77  {
78  luSeed = (ulong_t)time(NULL);
79  }
80 
81  seed[0] = (ushort_t)(luSeed & 0xffff);
82  seed[1] = (ushort_t)((luSeed >> 16) & 0xffff);
83  seed[2] = (ushort_t)(seed[0] << 8) | (ushort_t)(seed[1] >> 8);
84 
85  seed48_r(seed, &m_randState);
86 }
87 
88 int Random::randrange(int nMin, int nMax)
89 {
90  double lfRandVal;
91 
92  drand48_r(&m_randState, &lfRandVal);
93 
94  return (int)((double)(nMax - nMin) * lfRandVal + nMin);
95 }
96 
97 s64_t Random::randrange(s64_t nMin, s64_t nMax)
98 {
99  double lfRandVal;
100 
101  drand48_r(&m_randState, &lfRandVal);
102 
103  return (s64_t)((double)(nMax - nMin) * lfRandVal + nMin);
104 }
105 
106 float Random::uniform(float fMin, float fMax)
107 {
108  double lfRandVal;
109 
110  drand48_r(&m_randState, &lfRandVal);
111 
112  return (float)((double)(fMax - fMin) * lfRandVal + fMin);
113 }
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 generator class interface.
Random(ulong_t luSeed=AUTO_SEED)
Default initialization constructor.
Definition: Random.cxx:72
RoadNarrows Robotics.
Definition: Camera.h:74