libstdc++
random
Go to the documentation of this file.
00001 // <experimental/random> -*- C++ -*-
00002 
00003 // Copyright (C) 2015-2016 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 
00034 namespace std {
00035 namespace experimental {
00036 inline namespace fundamentals_v2 {
00037 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00038 
00039 #define __cpp_lib_experimental_randint 201511
00040 
00041   inline std::default_random_engine&
00042   _S_randint_engine()
00043   {
00044     static thread_local default_random_engine __eng{random_device{}()};
00045     return __eng;
00046   }
00047 
00048   // 13.2.2.1, Function template randint
00049   template<typename _IntType>
00050     inline _IntType
00051     randint(_IntType __a, _IntType __b)
00052     {
00053       static_assert(is_integral<_IntType>::value && sizeof(_IntType) > 1,
00054                     "argument must be an integer type");
00055       using _Dist = std::uniform_int_distribution<_IntType>;
00056       static thread_local _Dist __dist;
00057       return __dist(_S_randint_engine(), typename _Dist::param_type{__a, __b});
00058     }
00059 
00060   inline void
00061   reseed()
00062   {
00063     _S_randint_engine().seed(random_device{}());
00064   }
00065 
00066   inline void
00067   reseed(default_random_engine::result_type __value)
00068   {
00069     _S_randint_engine().seed(__value);
00070   }
00071 
00072 _GLIBCXX_END_NAMESPACE_VERSION
00073 } // namespace fundamentals_v2
00074 } // namespace experimental
00075 } // namespace std
00076 
00077 #endif