]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/util/testsuite_rng.h
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / util / testsuite_rng.h
CommitLineData
4569a895
AT
1// -*- C++ -*-
2
a945c346 3// Copyright (C) 2005-2024 Free Software Foundation, Inc.
4569a895
AT
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
748086b7 8// Foundation; either version 3, or (at your option) any later
4569a895
AT
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
4dc3fbb0 16// You should have received a copy of the GNU General Public License along
748086b7
JJ
17// with this library; see the file COPYING3. If not see
18// <http://www.gnu.org/licenses/>.
4569a895 19
4569a895
AT
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/**
2e3f9c21 33 * @file testsuite_rng.h
4569a895
AT
34 */
35
2e3f9c21
BK
36#ifndef _GLIBCXX_TESTSUITE_RNG_H
37#define _GLIBCXX_TESTSUITE_RNG_H
4569a895
AT
38
39#include <ctime>
2e3f9c21
BK
40#include <climits>
41#include <debug/debug.h>
4dc3fbb0 42#include <tr1/random>
4569a895 43
5e11f978 44namespace __gnu_pbds
4569a895
AT
45{
46 namespace test
47 {
48 class twister_rand_gen
49 {
4569a895 50 public:
f92ab29f 51 twister_rand_gen(unsigned int seed =
2e3f9c21
BK
52 static_cast<unsigned int>(std::time(0)))
53 : m_base_generator(seed)
54 {
55 // Do nothing.
56 }
4569a895
AT
57
58 void
2e3f9c21
BK
59 init(unsigned int seed)
60 { m_base_generator.seed(seed); }
4569a895 61
4dc3fbb0 62 static unsigned int
4569a895 63 get_time_determined_seed()
4dc3fbb0 64 { return(static_cast<unsigned int>(std::time(0))); }
4569a895
AT
65
66 unsigned long
f92ab29f 67 get_unsigned_long(unsigned long min = 0,
2e3f9c21
BK
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 }
4569a895
AT
76
77 double
2e3f9c21
BK
78 get_prob()
79 {
80 const double min = m_base_generator.min();
81 const double max = m_base_generator.max();
4ca4927a
JW
82 const double range = static_cast<double>(max - min);
83 const double res = static_cast<double>(m_base_generator() - min);
2e3f9c21
BK
84 const double ret = res / range;
85 _GLIBCXX_DEBUG_ASSERT(ret >= 0 && ret <= 1);
86 return ret;
87 }
4dc3fbb0
AT
88
89 private:
90 typedef std::tr1::mt19937 base_generator_t;
91
4dc3fbb0 92 base_generator_t m_base_generator;
4569a895
AT
93 };
94 } // namespace test
5e11f978 95} // namespace __gnu_pbds
4569a895 96
2e3f9c21 97#endif // #ifndef _GLIBCXX_TESTSUITE_RNG_H