RandomNumberGenerator.hh 4.87 KB
Newer Older
1
2
3
/*===========================================================================*\
 *                                                                           *
 *                               OpenMesh                                    *
Jan Möbius's avatar
Jan Möbius committed
4
 *      Copyright (C) 2001-2012 by Computer Graphics Group, RWTH Aachen      *
5
6
 *                           www.openmesh.org                                *
 *                                                                           *
Isaak Lim's avatar
Isaak Lim committed
7
 *---------------------------------------------------------------------------*
8
9
 *  This file is part of OpenMesh.                                           *
 *                                                                           *
Isaak Lim's avatar
Isaak Lim committed
10
 *  OpenMesh is free software: you can redistribute it and/or modify         *
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 *  it under the terms of the GNU Lesser General Public License as           *
 *  published by the Free Software Foundation, either version 3 of           *
 *  the License, or (at your option) any later version with the              *
 *  following exceptions:                                                    *
 *                                                                           *
 *  If other files instantiate templates or use macros                       *
 *  or inline functions from this file, or you compile this file and         *
 *  link it with other files to produce an executable, this file does        *
 *  not by itself cause the resulting executable to be covered by the        *
 *  GNU Lesser General Public License. This exception does not however       *
 *  invalidate any other reasons why the executable file might be            *
 *  covered by the GNU Lesser General Public License.                        *
 *                                                                           *
 *  OpenMesh is distributed in the hope that it will be useful,              *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
 *  GNU Lesser General Public License for more details.                      *
 *                                                                           *
 *  You should have received a copy of the GNU LesserGeneral Public          *
 *  License along with OpenMesh.  If not,                                    *
 *  see <http://www.gnu.org/licenses/>.                                      *
 *                                                                           *
Isaak Lim's avatar
Isaak Lim committed
33
\*===========================================================================*/
34
35

/*===========================================================================*\
Isaak Lim's avatar
Isaak Lim committed
36
 *                                                                           *
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
 *   $Revision: 693 $                                                         *
 *   $Date: 2012-09-23 16:25:16 +0200 (So, 23 Sep 2012) $                   *
 *                                                                           *
\*===========================================================================*/


//=============================================================================
//
//  Helper Functions for generating a random number between 0.0 and 1.0 with
//  a garantueed resolution
//
//=============================================================================


#ifndef OPENMESH_UTILS_RANDOMNUMBERGENERATOR_HH
#define OPENMESH_UTILS_RANDOMNUMBERGENERATOR_HH


//== INCLUDES =================================================================


#include <OpenMesh/Core/System/config.h>
59
60
#include <cstdlib>

61
62
63
64
65
66
67
68
69
70
71


//== NAMESPACES ===============================================================


namespace OpenMesh {


//=============================================================================


Jan Möbius's avatar
Jan Möbius committed
72
/**  Generate a random number between 0.0 and 1.0 with a guaranteed resolution
73
 *   ( Number of possible values )
Jan Möbius's avatar
Jan Möbius committed
74
75
76
 *
 * Especially useful on windows, as there MAX_RAND is often only 32k which is
 * not enough resolution for a lot of applications
77
78
79
 */
class OPENMESHDLLEXPORT RandomNumberGenerator
{
Isaak Lim's avatar
Isaak Lim committed
80
81
public:

82
  /** \brief Constructor
Isaak Lim's avatar
Isaak Lim committed
83
  *
84
85
  * @param _resolution specifies the desired resolution for the random number generated
  */
86
  RandomNumberGenerator(const size_t _resolution);
Isaak Lim's avatar
Isaak Lim committed
87

Jan Möbius's avatar
Jan Möbius committed
88
  /// returns a random double between 0.0 and 1.0 with a guaranteed resolution
89
90
  double getRand() const;

91
  size_t resolution() const;
Jan Möbius's avatar
Jan Möbius committed
92

93
private:
Isaak Lim's avatar
Isaak Lim committed
94

95
  /// desired resolution
96
  const size_t resolution_;
97
98

  /// number of "blocks" of RAND_MAX that make up the desired _resolution
99
  size_t iterations_;
100
101

  /// maximum random number generated, which is used for normalization
102
  size_t maxNum_;
103
104
105
106
107
108
109
110
};

//=============================================================================
} // namespace OpenMesh
//=============================================================================
#endif // OPENMESH_UTILS_RANDOMNUMBERGENERATOR_HH defined
//=============================================================================