Random.h 4.2 KB

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