You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.4KB

  1. // <experimental/random> -*- C++ -*-
  2. // Copyright (C) 2015-2020 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
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file experimental/random
  21. * This is a TS C++ Library header.
  22. * @ingroup libfund-ts
  23. */
  24. #ifndef _GLIBCXX_EXPERIMENTAL_RANDOM
  25. #define _GLIBCXX_EXPERIMENTAL_RANDOM 1
  26. #include <random>
  27. #include <experimental/bits/lfts_config.h>
  28. namespace std {
  29. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  30. namespace experimental {
  31. inline namespace fundamentals_v2 {
  32. #define __cpp_lib_experimental_randint 201511
  33. inline std::default_random_engine&
  34. _S_randint_engine()
  35. {
  36. static thread_local default_random_engine __eng{random_device{}()};
  37. return __eng;
  38. }
  39. // 13.2.2.1, Function template randint
  40. template<typename _IntType>
  41. inline _IntType
  42. randint(_IntType __a, _IntType __b)
  43. {
  44. static_assert(is_integral<_IntType>::value && sizeof(_IntType) > 1,
  45. "argument must be an integer type");
  46. using _Dist = std::uniform_int_distribution<_IntType>;
  47. // This relies on the fact our uniform_int_distribution is stateless,
  48. // otherwise we'd need a static thread_local _Dist and pass it
  49. // _Dist::param_type{__a, __b}.
  50. return _Dist(__a, __b)(_S_randint_engine());
  51. }
  52. inline void
  53. reseed()
  54. {
  55. _S_randint_engine().seed(random_device{}());
  56. }
  57. inline void
  58. reseed(default_random_engine::result_type __value)
  59. {
  60. _S_randint_engine().seed(__value);
  61. }
  62. } // namespace fundamentals_v2
  63. } // namespace experimental
  64. _GLIBCXX_END_NAMESPACE_VERSION
  65. } // namespace std
  66. #endif