spot
0.8.3
|
00001 // Copyright (C) 2004 Laboratoire d'Informatique de Paris 6 (LIP6), 00002 // département Systèmes Répartis Coopératifs (SRC), Université Pierre 00003 // et Marie Curie. 00004 // 00005 // This file is part of Spot, a model checking library. 00006 // 00007 // Spot is free software; you can redistribute it and/or modify it 00008 // under the terms of the GNU General Public License as published by 00009 // the Free Software Foundation; either version 2 of the License, or 00010 // (at your option) any later version. 00011 // 00012 // Spot is distributed in the hope that it will be useful, but WITHOUT 00013 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 00014 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 00015 // License for more details. 00016 // 00017 // You should have received a copy of the GNU General Public License 00018 // along with Spot; see the file COPYING. If not, write to the Free 00019 // Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 00020 // 02111-1307, USA. 00021 00022 #ifndef SPOT_MISC_RANDOM_HH 00023 # define SPOT_MISC_RANDOM_HH 00024 00025 # include <cmath> 00026 00027 namespace spot 00028 { 00032 00036 void srand(unsigned int seed); 00037 00042 int rrand(int min, int max); 00043 00048 int mrand(int max); 00049 00054 double drand(); 00055 00062 double nrand(); 00063 00069 double bmrand(); 00070 00082 template<double (*gen)()> 00083 class barand 00084 { 00085 public: 00086 barand(int n, double p) 00087 : n_(n), m_(n * p), s_(sqrt(n * p * (1 - p))) 00088 { 00089 } 00090 00091 int 00092 rand() const 00093 { 00094 int res; 00095 00096 for (;;) 00097 { 00098 double x = gen() * s_ + m_; 00099 if (x < 0.0) 00100 continue; 00101 res = static_cast<int> (x); 00102 if (res <= n_) 00103 break; 00104 } 00105 return res; 00106 } 00107 protected: 00108 const int n_; 00109 const double m_; 00110 const double s_; 00111 }; 00112 00117 int prand(double p); 00119 } 00120 00121 #endif // SPOT_MISC_RANDOM_HH