diff --git a/ChangeLog b/ChangeLog index 953f16c79..529bbd582 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2004-11-12 Alexandre Duret-Lutz + + * misc/random.cc, misc/random.hh: New files. + * src/misc/Makefile.am (misc_HEADERS, libmisc_la_SOURCES): Add them. + 2004-11-10 Alexandre Duret-Lutz * src/evtgbaparse/public.hh (evtgba_parse): Fix doxygen comments. diff --git a/src/misc/Makefile.am b/src/misc/Makefile.am index b04fc738d..43b87fdd4 100644 --- a/src/misc/Makefile.am +++ b/src/misc/Makefile.am @@ -33,6 +33,7 @@ misc_HEADERS = \ hash.hh \ minato.hh \ modgray.hh \ + random.hh \ version.hh noinst_LTLIBRARIES = libmisc.la @@ -43,4 +44,5 @@ libmisc_la_SOURCES = \ freelist.cc \ minato.cc \ modgray.cc \ + random.cc \ version.cc diff --git a/src/misc/random.cc b/src/misc/random.cc new file mode 100644 index 000000000..8fede2fda --- /dev/null +++ b/src/misc/random.cc @@ -0,0 +1,54 @@ +// Copyright (C) 2004 Laboratoire d'Informatique de Paris 6 (LIP6), +// département Systèmes Répartis Coopératifs (SRC), Université Pierre +// et Marie Curie. +// +// This file is part of Spot, a model checking library. +// +// Spot is free software; you can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// Spot 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 Spot; see the file COPYING. If not, write to the Free +// Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA +// 02111-1307, USA. + +#include "random.hh" +#include + +namespace spot +{ + void + srand(unsigned int seed) + { + ::srand(seed); + } + + double + drand() + { + double r = ::rand(); + return r / (1.0 + RAND_MAX); + } + + int + mrand(int max) + { + int res = static_cast(max * drand()); + return res; + } + + + int + rrand(int min, int max) + { + return min + static_cast((max - min + 1) * drand()); + } + +} diff --git a/src/misc/random.hh b/src/misc/random.hh new file mode 100644 index 000000000..c9837317b --- /dev/null +++ b/src/misc/random.hh @@ -0,0 +1,46 @@ +// Copyright (C) 2004 Laboratoire d'Informatique de Paris 6 (LIP6), +// département Systèmes Répartis Coopératifs (SRC), Université Pierre +// et Marie Curie. +// +// This file is part of Spot, a model checking library. +// +// Spot is free software; you can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// Spot 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 Spot; see the file COPYING. If not, write to the Free +// Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA +// 02111-1307, USA. + +namespace spot +{ + /// \brief Reset the seed of the pseudo-random number generator. + /// + /// \see drand, mrand, rrand + void srand(unsigned int seed); + + /// \brief Compute a pseudo-random integer value between \a min and + /// \a max included. + /// + /// \see drand, mrand, srand + int rrand(int min, int max); + + /// \brief Compute a pseudo-random integer value between 0 and + /// \a max-1 included. + /// + /// \see drand, rrand, srand + int mrand(int max); + + /// \brief Compute a pseudo-random double value + /// between 0.0 and 1.0 (1.0 excluded). + /// + /// \see mrand, rrand, srand + double drand(); +}