BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/Random.h
Go to the documentation of this file.
00001 // Copyright (c) 1997-2001  Utrecht University (The Netherlands),
00002 // ETH Zurich (Switzerland), Freie Universitaet Berlin (Germany),
00003 // INRIA Sophia-Antipolis (France), Martin-Luther-University Halle-Wittenberg
00004 // (Germany), Max-Planck-Institute Saarbruecken (Germany), RISC Linz (Austria),
00005 // and Tel-Aviv University (Israel).  All rights reserved.
00006 //
00007 // This file is part of CGAL (www.cgal.org); you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public License as
00009 // published by the Free Software Foundation; version 2.1 of the License.
00010 // See the file LICENSE.LGPL distributed with CGAL.
00011 //
00012 // Licensees holding a valid commercial license may use this file in
00013 // accordance with the commercial license agreement provided with the software.
00014 //
00015 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00016 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00017 //
00018 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Random_numbers/include/CGAL/Random.h $
00019 // $Id: Random.h 46871 2008-11-13 14:40:48Z gaertner $
00020 // 
00021 //
00022 // Author(s)     : Sven Schoenherr <sven@inf.ethz.ch>, Sylvain Pion
00023 
00024 #ifndef CGAL_RANDOM_H
00025 #define CGAL_RANDOM_H
00026 
00027 #include <utility>
00028 #include <CGAL/basic.h>
00029 
00030 CGAL_BEGIN_NAMESPACE
00031 
00032 class Random {
00033   public:
00034     // types
00035     typedef std::pair<unsigned int, unsigned int> State;
00036 
00037     // creation
00038     Random( );
00039     Random( unsigned int  seed);
00040 
00041     // seed
00042     unsigned int get_seed ( ) const;
00043     
00044     // operations
00045     bool    get_bool  ( );
00046     int     get_int   ( int lower, int upper);
00047     double  get_double( double lower = 0.0, double upper = 1.0);
00048 
00049     // state 
00050     void save_state( State& state) const;
00051     void restore_state( const State& state);
00052 
00053     // Computes a random int value smaller than 2^b.
00054     // It's supposed to be fast, useful for randomized algorithms.
00055     // The distribution is not perfectly flat, but this is a sacrifice against
00056     // efficiency.
00057     template <int b>
00058     int get_bits()
00059     {
00060         CGAL_assertion(0<b && b<16);
00061         if (val == 0) {
00062             random_value = (421U * random_value + 2073U) % 32749U;
00063             val = random_value;
00064         }
00065         int ret = val & ((1<<b)-1);
00066         val >>= 1; // Shifting by b would be slightly better, but is slower.
00067         return ret;
00068     }
00069 
00070     int     operator () ( int upper);
00071 
00072   bool    operator==(Random rd) const
00073   {
00074     return 
00075       rd.rand_max_plus_1 == rand_max_plus_1 &&
00076       rd.random_value == random_value &&
00077       rd.val == val;
00078   }
00079   private:
00080     // data members
00081     const double  rand_max_plus_1;
00082     unsigned int random_value; // Current 15 bits random value.
00083     unsigned int val; // random_value shifted by used bits.
00084     unsigned int seed; 
00085 };
00086 
00087 // Global variables
00088 // ================
00089 extern  Random  default_random;
00090 
00091 CGAL_END_NAMESPACE
00092 
00093 // ============================================================================
00094 
00095 // Class implementation (inline functions)
00096 // =======================================
00097 // includes
00098 #  include <cstdlib>
00099 
00100 CGAL_BEGIN_NAMESPACE
00101 
00102 // operations
00103 inline
00104 bool
00105 Random::
00106 get_bool( )
00107 {
00108     return( static_cast< bool>( std::rand() & 1));
00109 }
00110 
00111 inline
00112 int
00113 Random::
00114 get_int( int lower, int upper)
00115 {
00116     return( lower + static_cast< int>(
00117       ( static_cast< double>( upper) - lower) * 
00118       std::rand() / rand_max_plus_1));
00119 }
00120 
00121 inline
00122 double
00123 Random::
00124 get_double( double lower, double upper)
00125 {
00126     return( lower + ( ( upper-lower) * 
00127                       std::rand() / rand_max_plus_1));
00128 }
00129 
00130 inline
00131 int
00132 Random::
00133 operator () ( int upper)
00134 {
00135     return( get_int( 0, upper));
00136 }
00137 
00138 CGAL_END_NAMESPACE
00139 
00140 #endif // CGAL_RANDOM_H
00141 
00142 // ===== EOF ==================================================================
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines