00001 /*************************************************************************** 00002 * Copyright (C) 2005 by Robot Group Leipzig * 00003 * martius@informatik.uni-leipzig.de * 00004 * fhesse@informatik.uni-leipzig.de * 00005 * der@informatik.uni-leipzig.de * 00006 * joergweide84@aol.com (robot12) * 00007 * * 00008 * This program is free software; you can redistribute it and/or modify * 00009 * it under the terms of the GNU General Public License as published by * 00010 * the Free Software Foundation; either version 2 of the License, or * 00011 * (at your option) any later version. * 00012 * * 00013 * This program is distributed in the hope that it will be useful, * 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00016 * GNU General Public License for more details. * 00017 * * 00018 * You should have received a copy of the GNU General Public License * 00019 * along with this program; if not, write to the * 00020 * Free Software Foundation, Inc., * 00021 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 00022 * * 00023 * Random generator with internal state used for multitheading envs. * 00024 * * 00025 * $Log: randomgenerator.h,v $ 00026 * Revision 1.10 2009/10/29 15:23:55 martius 00027 * use GNU_SOURCE 00028 * 00029 * Revision 1.9 2009/10/29 14:51:14 fhesse 00030 * *** empty log message *** 00031 * 00032 * Revision 1.8 2009/10/29 14:23:36 martius 00033 * random gen also for mac (non-gnu) 00034 * 00035 * Revision 1.7 2009/07/23 13:25:12 guettler 00036 * cosmetic 00037 * 00038 * Revision 1.6 2009/07/23 13:24:03 guettler 00039 * comments corrected (spelling, grammar) 00040 * 00041 * Revision 1.5 2009/07/22 13:21:14 robot12 00042 * bugfix in randomgenerator. so it should be work on a 64bit system. 00043 * see comment in line 61 for the union around the buffer. 00044 * 00045 * Revision 1.4 2009/07/21 08:47:33 robot12 00046 * add some comments 00047 * 00048 * Revision 1.3 2009/07/15 08:33:58 guettler 00049 * workaround for bug: drand48_r overwrites too much data 00050 * - buffer increased (thanks to Joern Hoffmann) 00051 * 00052 * Revision 1.2 2008/04/17 14:54:45 martius 00053 * randomGen added, which is a random generator with long period and an 00054 * internal state. Each Agent has an instance and passed it to the controller 00055 * and the wiring. This is good for 00056 * a) repeatability on agent basis, 00057 * b) parallel execution as done in ode_robots 00058 * 00059 * Revision 1.1 2008/04/15 17:03:34 martius 00060 * random generator with state 00061 * 00062 ***************************************************************************/ 00063 #ifndef __RANDOMGENERATOR_H 00064 #define __RANDOMGENERATOR_H 00065 00066 #include <stdlib.h> 00067 #ifndef _GNU_SOURCE 00068 #include "mac_drand48r.h" 00069 #endif 00070 00071 typedef struct _RandGen { 00072 _RandGen(){ 00073 init(::rand()); 00074 } 00075 void init(long int seedval){ 00076 srand48_r(seedval, &buffer); 00077 } 00078 double rand(){ 00079 double r; 00080 drand48_r(&buffer,&r); 00081 return r; 00082 } 00083 // See drand48_data structure: 00084 // struct drand48_data 00085 // { 00086 // unsigned short int __x[3]; /* Current state. */ 00087 // unsigned short int __old_x[3]; /* Old state. */ 00088 // unsigned short int __c; /* Additive const. in congruential formula. */ 00089 // unsigned short int __init; /* Flag for initializing. */ 00090 // unsigned long long int __a; /* Factor in congruential formula. */ 00091 // }; 00092 // 00093 // The function drand48_r writes too much data into __x. 00094 // I think it writes 16 bytes in the 8 byte sized array. 00095 // Therefore this destroys the call stack. 00096 // With a union and a char array of 24 Bytes we can force him 00097 // to write all elements in the structure to be aligned in memory 00098 // which is not ensured if you have a struct. 00099 // So the 16 bytes are written into __x and __old_x, the call stack is save. 00100 // This isn't the best way, but everything which isn't predictable 00101 // is in a random generator perfect. :o) 00102 union { 00103 struct drand48_data buffer; 00104 char dummy[24]; 00105 }; 00106 } RandGen; 00107 00108 00109 00110 00111 #endif