OSDN Git Service

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