00001 /* vim: set sw=4 sts=4 et foldmethod=syntax : */ 00002 00003 /* 00004 * Copyright (c) 2006, 2007, 2008, 2009 Ciaran McCreesh 00005 * 00006 * This file is part of the Paludis package manager. Paludis is free software; 00007 * you can redistribute it and/or modify it under the terms of the GNU General 00008 * Public License version 2, as published by the Free Software Foundation. 00009 * 00010 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY 00011 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00012 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 00013 * details. 00014 * 00015 * You should have received a copy of the GNU General Public License along with 00016 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple 00017 * Place, Suite 330, Boston, MA 02111-1307 USA 00018 */ 00019 00020 #ifndef PALUDIS_GUARD_PALUDIS_UTIL_RANDOM_HH 00021 #define PALUDIS_GUARD_PALUDIS_UTIL_RANDOM_HH 1 00022 00023 #include <cstdlib> 00024 #include <inttypes.h> 00025 #include <paludis/util/attributes.hh> 00026 00027 /** \file 00028 * Declarations for the Random class. 00029 * 00030 * \ingroup g_utils 00031 * 00032 * \section Examples 00033 * 00034 * - None at this time. 00035 */ 00036 00037 namespace paludis 00038 { 00039 /** 00040 * A basic random number generator class, which is not suitable for 00041 * cryptography but is fast and reasonably pseudorandom. 00042 * 00043 * See \ref TCppPL 22.7 for justification. See \ref TaoCP2 3.2.1 for the 00044 * basic algorithm and \ref AppCrypt 16.1 for the choice of numbers. 00045 * 00046 * \ingroup g_utils 00047 * \nosubgrouping 00048 */ 00049 class PALUDIS_VISIBLE Random 00050 { 00051 private: 00052 static uint32_t global_seed; 00053 uint32_t local_seed; 00054 00055 static const uint32_t _a = 2416; 00056 static const uint32_t _b = 374441; 00057 static const uint32_t _m = 1771875; 00058 00059 public: 00060 ///\name Basic operations 00061 ///\{ 00062 00063 /// Constructor, with a seed. 00064 Random(uint32_t seed); 00065 00066 /// Constructor, with a magic random seed. 00067 Random(); 00068 00069 ///\} 00070 00071 /// Fetch a random number in (0, max] 00072 template <typename DiffType_> 00073 DiffType_ operator() (DiffType_ max) 00074 { 00075 local_seed = (_a * local_seed + _b) % _m; 00076 double t(static_cast<double>(local_seed) / static_cast<double>(_m)); 00077 return static_cast<DiffType_>(t * max); 00078 } 00079 }; 00080 } 00081 00082 #endif