libstdc++
random
Go to the documentation of this file.
00001 // <experimental/random> -*- C++ -*-
00002 
00003 // Copyright (C) 2015-2017 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file experimental/random
00026  *  This is a TS C++ Library header.
00027  */
00028 
00029 #ifndef _GLIBCXX_EXPERIMENTAL_RANDOM
00030 #define _GLIBCXX_EXPERIMENTAL_RANDOM 1
00031 
00032 #include <random>
00033 #include <experimental/bits/lfts_config.h>
00034 
00035 namespace std {
00036 namespace experimental {
00037 inline namespace fundamentals_v2 {
00038 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00039 
00040 #define __cpp_lib_experimental_randint 201511
00041 
00042   inline std::default_random_engine&
00043   _S_randint_engine()
00044   {
00045     static thread_local default_random_engine __eng{random_device{}()};
00046     return __eng;
00047   }
00048 
00049   // 13.2.2.1, Function template randint
00050   template<typename _IntType>
00051     inline _IntType
00052     randint(_IntType __a, _IntType __b)
00053     {
00054       static_assert(is_integral<_IntType>::value && sizeof(_IntType) > 1,
00055                     "argument must be an integer type");
00056       using _Dist = std::uniform_int_distribution<_IntType>;
00057       // This relies on the fact our uniform_int_distribution is stateless,
00058       // otherwise we'd need a static thread_local _Dist and pass it
00059       // _Dist::param_type{__a, __b}.
00060       return _Dist(__a, __b)(_S_randint_engine());
00061     }
00062 
00063   inline void
00064   reseed()
00065   {
00066     _S_randint_engine().seed(random_device{}());
00067   }
00068 
00069   inline void
00070   reseed(default_random_engine::result_type __value)
00071   {
00072     _S_randint_engine().seed(__value);
00073   }
00074 
00075 _GLIBCXX_END_NAMESPACE_VERSION
00076 } // namespace fundamentals_v2
00077 } // namespace experimental
00078 } // namespace std
00079 
00080 #endif