testsuite_rng.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // -*- C++ -*-
  2. // Copyright (C) 2005-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the terms
  6. // of the GNU General Public License as published by the Free Software
  7. // Foundation; either version 3, or (at your option) any later
  8. // version.
  9. // This library is distributed in the hope that it will be useful, but
  10. // WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // General Public License for more details.
  13. // You should have received a copy of the GNU General Public License along
  14. // with this library; see the file COPYING3. If not see
  15. // <http://www.gnu.org/licenses/>.
  16. // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
  17. // Permission to use, copy, modify, sell, and distribute this software
  18. // is hereby granted without fee, provided that the above copyright
  19. // notice appears in all copies, and that both that copyright notice
  20. // and this permission notice appear in supporting documentation. None
  21. // of the above authors, nor IBM Haifa Research Laboratories, make any
  22. // representation about the suitability of this software for any
  23. // purpose. It is provided "as is" without express or implied
  24. // warranty.
  25. /**
  26. * @file testsuite_rng.h
  27. */
  28. #ifndef _GLIBCXX_TESTSUITE_RNG_H
  29. #define _GLIBCXX_TESTSUITE_RNG_H
  30. #include <ctime>
  31. #include <climits>
  32. #include <debug/debug.h>
  33. #include <tr1/random>
  34. namespace __gnu_pbds
  35. {
  36. namespace test
  37. {
  38. class twister_rand_gen
  39. {
  40. public:
  41. twister_rand_gen(unsigned int seed =
  42. static_cast<unsigned int>(std::time(0)))
  43. : m_base_generator(seed)
  44. {
  45. // Do nothing.
  46. }
  47. void
  48. init(unsigned int seed)
  49. { m_base_generator.seed(seed); }
  50. static unsigned int
  51. get_time_determined_seed()
  52. { return(static_cast<unsigned int>(std::time(0))); }
  53. unsigned long
  54. get_unsigned_long(unsigned long min = 0,
  55. unsigned long max = UINT_MAX - 1)
  56. {
  57. _GLIBCXX_DEBUG_ASSERT(max >= min);
  58. const double prob = get_prob();
  59. const unsigned long r = (unsigned long)((max - min + 1) * prob) + min;
  60. _GLIBCXX_DEBUG_ASSERT(r <= max);
  61. return r;
  62. }
  63. double
  64. get_prob()
  65. {
  66. const double min = m_base_generator.min();
  67. const double max = m_base_generator.max();
  68. const double range = static_cast<double>(max - min);
  69. const double res = static_cast<double>(m_base_generator() - min);
  70. const double ret = res / range;
  71. _GLIBCXX_DEBUG_ASSERT(ret >= 0 && ret <= 1);
  72. return ret;
  73. }
  74. private:
  75. typedef std::tr1::mt19937 base_generator_t;
  76. base_generator_t m_base_generator;
  77. };
  78. } // namespace test
  79. } // namespace __gnu_pbds
  80. #endif // #ifndef _GLIBCXX_TESTSUITE_RNG_H