1 | // |
2 | // Random.h |
3 | // |
4 | // Library: Foundation |
5 | // Package: Crypt |
6 | // Module: Random |
7 | // |
8 | // Definition of class Random. |
9 | // |
10 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
11 | // and Contributors. |
12 | // |
13 | // SPDX-License-Identifier: BSL-1.0 |
14 | // |
15 | // |
16 | // Based on the FreeBSD random number generator. |
17 | // src/lib/libc/stdlib/random.c,v 1.25 |
18 | // |
19 | // Copyright (c) 1983, 1993 |
20 | // The Regents of the University of California. All rights reserved. |
21 | // Redistribution and use in source and binary forms, with or without |
22 | // modification, are permitted provided that the following conditions |
23 | // are met: |
24 | // 1. Redistributions of source code must retain the above copyright |
25 | // notice, this list of conditions and the following disclaimer. |
26 | // 2. Redistributions in binary form must reproduce the above copyright |
27 | // notice, this list of conditions and the following disclaimer in the |
28 | // documentation and/or other materials provided with the distribution. |
29 | // 4. Neither the name of the University nor the names of its contributors |
30 | // may be used to endorse or promote products derived from this software |
31 | // without specific prior written permission. |
32 | // |
33 | // THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
34 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
35 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
36 | // ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
37 | // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
38 | // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
39 | // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
40 | // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
41 | // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
42 | // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
43 | // SUCH DAMAGE. |
44 | // |
45 | |
46 | |
47 | #ifndef Foundation_Random_INCLUDED |
48 | #define Foundation_Random_INCLUDED |
49 | |
50 | |
51 | #include "Poco/Foundation.h" |
52 | |
53 | |
54 | namespace Poco { |
55 | |
56 | |
57 | class Foundation_API Random |
58 | /// A better random number generator. |
59 | /// Random implements a pseudo random number generator |
60 | /// (PRNG). The PRNG is a nonlinear additive |
61 | /// feedback random number generator using 256 bytes |
62 | /// of state information and a period of up to 2^69. |
63 | { |
64 | public: |
65 | enum Type |
66 | { |
67 | RND_STATE_0 = 8, /// linear congruential |
68 | RND_STATE_32 = 32, /// x**7 + x**3 + 1 |
69 | RND_STATE_64 = 64, /// x**15 + x + 1 |
70 | RND_STATE_128 = 128, /// x**31 + x**3 + 1 |
71 | RND_STATE_256 = 256 /// x**63 + x + 1 |
72 | }; |
73 | |
74 | Random(int stateSize = 256); |
75 | /// Creates and initializes the PRNG. |
76 | /// Specify either a state buffer size |
77 | /// (8 to 256 bytes) or one of the Type values. |
78 | |
79 | ~Random(); |
80 | /// Destroys the PRNG. |
81 | |
82 | void seed(UInt32 seed); |
83 | /// Seeds the pseudo random generator with the given seed. |
84 | |
85 | void seed(); |
86 | /// Seeds the pseudo random generator with a random seed |
87 | /// obtained from a RandomInputStream. |
88 | |
89 | UInt32 next(); |
90 | /// Returns the next 31-bit pseudo random number. |
91 | |
92 | UInt32 next(UInt32 n); |
93 | /// Returns the next 31-bit pseudo random number modulo n. |
94 | |
95 | char nextChar(); |
96 | /// Returns the next pseudo random character. |
97 | |
98 | bool nextBool(); |
99 | /// Returns the next boolean pseudo random value. |
100 | |
101 | float nextFloat(); |
102 | /// Returns the next float pseudo random number between 0.0 and 1.0. |
103 | |
104 | double nextDouble(); |
105 | /// Returns the next double pseudo random number between 0.0 and 1.0. |
106 | |
107 | protected: |
108 | void initState(UInt32 seed, char* arg_state, Int32 n); |
109 | static UInt32 goodRand(Int32 x); |
110 | |
111 | private: |
112 | enum |
113 | { |
114 | MAX_TYPES = 5, |
115 | NSHUFF = 50 |
116 | }; |
117 | |
118 | UInt32* _fptr; |
119 | UInt32* _rptr; |
120 | UInt32* _state; |
121 | int _randType; |
122 | int _randDeg; |
123 | int _randSep; |
124 | UInt32* _endPtr; |
125 | char* _pBuffer; |
126 | }; |
127 | |
128 | |
129 | // |
130 | // inlines |
131 | // |
132 | inline UInt32 Random::next(UInt32 n) |
133 | { |
134 | return next() % n; |
135 | } |
136 | |
137 | |
138 | inline char Random::nextChar() |
139 | { |
140 | return char((next() >> 3) & 0xFF); |
141 | } |
142 | |
143 | |
144 | inline bool Random::nextBool() |
145 | { |
146 | return (next() & 0x1000) != 0; |
147 | } |
148 | |
149 | |
150 | inline float Random::nextFloat() |
151 | { |
152 | return float(next()) / 0x7FFFFFFF; |
153 | } |
154 | |
155 | |
156 | inline double Random::nextDouble() |
157 | { |
158 | return double(next()) / 0x7FFFFFFF; |
159 | } |
160 | |
161 | |
162 | } // namespace Poco |
163 | |
164 | |
165 | #endif // Foundation_Random_INCLUDED |
166 | |