3 // Copyright (C) 2005-2025 Free Software Foundation, Inc.
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
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.
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/>.
21 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
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
33 * @file testsuite_rng.h
36 #ifndef _GLIBCXX_TESTSUITE_RNG_H
37 #define _GLIBCXX_TESTSUITE_RNG_H
41 #include <debug/debug.h>
48 class twister_rand_gen
51 twister_rand_gen(unsigned int seed
=
52 static_cast<unsigned int>(std::time(0)))
53 : m_base_generator(seed
)
59 init(unsigned int seed
)
60 { m_base_generator
.seed(seed
); }
63 get_time_determined_seed()
64 { return(static_cast<unsigned int>(std::time(0))); }
67 get_unsigned_long(unsigned long min
= 0,
68 unsigned long max
= UINT_MAX
- 1)
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
);
80 const double min
= m_base_generator
.min();
81 const double max
= m_base_generator
.max();
82 const double range
= static_cast<double>(max
- min
);
83 const double res
= static_cast<double>(m_base_generator() - min
);
84 const double ret
= res
/ range
;
85 _GLIBCXX_DEBUG_ASSERT(ret
>= 0 && ret
<= 1);
90 typedef std::tr1::mt19937 base_generator_t
;
92 base_generator_t m_base_generator
;
95 } // namespace __gnu_pbds
97 #endif // #ifndef _GLIBCXX_TESTSUITE_RNG_H