Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

coconut_random.h

Go to the documentation of this file.
00001 // Random for COCONUT implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2003 Hermann Schichl
00004 //
00005 // This file is part of the COCONUT API.  This library
00006 // is free software; you can redistribute it and/or modify it under the
00007 // terms of the Library GNU General Public License as published by the
00008 // Free Software Foundation; either version 2, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // Library GNU General Public License for more details.
00015 
00016 // As a special exception, you may use this file as part of a free software
00017 // library without restriction.  Specifically, if other files instantiate
00018 // templates or use macros or inline functions from this file, or you compile
00019 // this file and link it with other files to produce an executable, this
00020 // file does not by itself cause the resulting executable to be covered by
00021 // the Library GNU General Public License.  This exception does not however
00022 // invalidate any other reasons why the executable file might be covered by
00023 // the Library GNU General Public License.
00024 
00027 #ifndef __COCONUT_RANDOM_H__
00028 #define __COCONUT_RANDOM_H__
00029 
00030 #ifndef DEBUG_RANDOM
00031 #define DEBUG_RANDOM 1
00032 #endif
00033 
00034 #include <cmath>
00035 #include <cstdlib>
00036 
00037 #define coconut_random  random
00038 #define coconut_seed(A) srandom(A)
00039 #define COCONUT_RAND_MAX RAND_MAX
00040 
00041 #define COCONUT_RRAND_MIN -1.e08
00042 #define COCONUT_RRAND_MAX +1.e08
00043 
00044 #if DEBUG_RANDOM
00045 #define INIT_SEED    1
00046 #else
00047 #define INIT_SEED    random()
00048 #endif
00049 
00050 #define coconut_init_random() srandom(INIT_SEED)
00051 
00052 typedef long int rand_t;
00053 
00054 inline double d_random()
00055 {
00056 #if (DEBUG_RANDOM > 3)
00057   double help = (coconut_random()+0.)/(COCONUT_RAND_MAX+0.);
00058   std::cout << "Random generated: " << help << std::endl;
00059   return help;
00060 #else
00061   return (coconut_random()+0.)/(COCONUT_RAND_MAX+0.);
00062 #endif
00063 }
00064 
00065 inline double r_random(double l, double u)
00066 {
00067   l = std::max(l,COCONUT_RRAND_MIN);
00068   u = std::min(u,COCONUT_RRAND_MAX);
00069   if(l == u) return l;
00070   double rndnum = d_random();
00071   if(l >= 0)
00072     u = u-l;
00073   else if(u <= 0)
00074   {
00075     double help = l-u;
00076     l = u;
00077     u = help;
00078   }
00079   else
00080   {
00081     double help = l;
00082     l = 0;
00083     rndnum = 2*(rndnum-0.5);
00084     if(rndnum < 0)
00085       u = -help;
00086   }
00087 #if (DEBUG_RANDOM > 3)
00088   std::cout << "r_random: " << l << ", " << u << ", " << rndnum << std::endl;
00089 #endif
00090   return tan(rndnum*atan(u))+l;
00091 }
00092 
00093 inline double r_random(const interval& _i)
00094 {
00095   return r_random(_i.inf(), _i.sup());
00096 }
00097 
00098 #endif /* __COCONUT_RANDOM_H__ */

Generated on Tue Nov 4 01:57:56 2003 for COCONUT API by doxygen1.2.18