aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2006-04-01 22:50:04 +0000
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2006-04-01 22:50:04 +0000
commit6da4ca368cf24c9b6c0a16bf09e296bd20ea8596 (patch)
tree1bd2f5b709d1e8dfff6cf49e80ef38c0961c2ed7
parent76f7576840010da4848f29c6ac7bdc11d039b09c (diff)
downloadpaludis-6da4ca368cf24c9b6c0a16bf09e296bd20ea8596.tar.gz
paludis-6da4ca368cf24c9b6c0a16bf09e296bd20ea8596.tar.xz
Move rng to a class. Said class currently sucks, but it doesn't really matter.
-rw-r--r--paludis/portage_repository.cc4
-rw-r--r--paludis/util/files.m41
-rw-r--r--paludis/util/random.hh50
3 files changed, 54 insertions, 1 deletions
diff --git a/paludis/portage_repository.cc b/paludis/portage_repository.cc
index 2b92752..b76f5b1 100644
--- a/paludis/portage_repository.cc
+++ b/paludis/portage_repository.cc
@@ -34,6 +34,7 @@
#include <paludis/util/is_file_with_extension.hh>
#include <paludis/util/log.hh>
#include <paludis/util/pstream.hh>
+#include <paludis/util/random.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/strip.hh>
#include <paludis/util/tokeniser.hh>
@@ -890,7 +891,8 @@ PortageRepository::do_is_mirror(const std::string & s) const
{
/* pick up to five random mirrors only */
/// \todo param this
- std::random_shuffle(next(entries.begin()), entries.end());
+ static Random r;
+ std::random_shuffle(next(entries.begin()), entries.end(), r);
if (entries.size() > 6)
entries.resize(6);
_imp->mirrors.insert(std::make_pair(
diff --git a/paludis/util/files.m4 b/paludis/util/files.m4
index 3e1e277..e15b08e 100644
--- a/paludis/util/files.m4
+++ b/paludis/util/files.m4
@@ -29,6 +29,7 @@ add(`log', `hh', `cc', `test')
add(`match_sequence', `hh', `cc', `test')
add(`private_implementation_pattern', `hh', `cc')
add(`pstream', `hh', `cc', `test')
+add(`random', `hh', `cc')
add(`save', `hh', `cc', `test')
add(`smart_record', `hh', `test')
add(`stringify', `hh', `test')
diff --git a/paludis/util/random.hh b/paludis/util/random.hh
new file mode 100644
index 0000000..8c47dcf
--- /dev/null
+++ b/paludis/util/random.hh
@@ -0,0 +1,50 @@
+/* vim: set sw=4 sts=4 et foldmethod=syntax : */
+
+/*
+ * Copyright (c) 2006 Ciaran McCreesh <ciaranm@gentoo.org>
+ *
+ * This file is part of the Paludis package manager. Paludis is free software;
+ * you can redistribute it and/or modify it under the terms of the GNU General
+ * Public License version 2, as published by the Free Software Foundation.
+ *
+ * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+ * Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef PALUDIS_GUARD_PALUDIS_UTIL_RANDOM_HH
+#define PALUDIS_GUARD_PALUDIS_UTIL_RANDOM_HH 1
+
+#include <cstdlib>
+#include <time.h>
+
+namespace paludis
+{
+ /// \todo Don't use cstdlib functions here.
+ class Random
+ {
+ private:
+ static bool done_srand;
+
+ public:
+ template <typename DiffType_>
+ DiffType_ operator() (DiffType_ max)
+ {
+ if (! done_srand)
+ {
+ std::srand(time(0));
+ done_srand = true;
+ }
+
+ double t(static_cast<double>(std::rand()) / static_cast<double>(RAND_MAX));
+ return static_cast<DiffType_>(t * max);
+ }
+ };
+}
+
+#endif