Robot Simulator of the Robotics Group for Self-Organization of Control  0.8.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
randomgenerator.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2011 LpzRobots development team *
3  * Georg Martius <georg dot martius at web dot de> *
4  * Frank Guettler <guettler at informatik dot uni-leipzig dot de *
5  * Frank Hesse <frank at nld dot ds dot mpg dot de> *
6  * Ralf Der <ralfder at mis dot mpg dot de> *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the *
20  * Free Software Foundation, Inc., *
21  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22  * *
23  ***************************************************************************/
24 #ifndef __RANDOMGENERATOR_H
25 #define __RANDOMGENERATOR_H
26 
27 #include <stdlib.h>
28 #ifndef _GNU_SOURCE
29 #include "mac_drand48r.h"
30 #endif
31 
32 
33 /// random generator with 48bit integer arithmentic
34 typedef struct _RandGen {
36  init(::rand());
37  }
38  void init(long int seedval){
39  srand48_r(seedval, &buffer);
40  }
41  /// returns a value in [0,1)
42  double rand(){
43  double r;
44  drand48_r(&buffer,&r);
45  return r;
46  }
47  // See drand48_data structure:
48  // struct drand48_data
49  // {
50  // unsigned short int __x[3]; /* Current state. */
51  // unsigned short int __old_x[3]; /* Old state. */
52  // unsigned short int __c; /* Additive const. in congruential formula. */
53  // unsigned short int __init; /* Flag for initializing. */
54  // unsigned long long int __a; /* Factor in congruential formula. */
55  // };
56  //
57  // The function drand48_r writes too much data into __x.
58  // I think it writes 16 bytes in the 8 byte sized array.
59  // Therefore this destroys the call stack.
60  // With a union and a char array of 24 Bytes we can force him
61  // to write all elements in the structure to be aligned in memory
62  // which is not ensured if you have a struct.
63  // So the 16 bytes are written into __x and __old_x, the call stack is save.
64  // This isn't the best way, but everything which isn't predictable
65  // is in a random generator perfect. :o)
66  union {
68  char dummy[24];
69  };
70 } RandGen;
71 
72 
73 
74 
75 #endif
Definition: mac_drand48r.h:63
struct drand48_data buffer
Definition: randomgenerator.h:67
double rand()
returns a value in [0,1)
Definition: randomgenerator.h:42
int drand48_r(struct drand48_data *buffer, double *result)
Definition: randomgenerator.cpp:104
random generator with 48bit integer arithmentic
Definition: randomgenerator.h:34
struct _RandGen RandGen
random generator with 48bit integer arithmentic
void init(long int seedval)
Definition: randomgenerator.h:38
int srand48_r(long int seedval, struct drand48_data *buffer)
Definition: randomgenerator.cpp:87
char dummy[24]
Definition: randomgenerator.h:68
_RandGen()
Definition: randomgenerator.h:35