LCFIVertex  0.7.2
RandomNumberUtils.h
1 #ifndef RANDOMNUMBERUTILS_H
2 #define RANDOMNUMBERUTILS_H
3 
4 #include "NeuralNetConfig.h"
5 
6 #include <ctime>
7 #include <cstdlib>
8 #include <cmath>
9 
10 // Utility functions for random numbers.
11 // Default implementation uses rand(), so
12 // it should almost certainly be replaced
13 // with something better!
14 
15 //namespace nnet added 15/08/06 by Mark Grimes (mark.grimes@bristol.ac.uk) for the LCFI vertex package
16 namespace nnet
17 {
18 
19 namespace NeuralNetRandom {
20 
21 inline unsigned int GetNewRandomSeed(void)
22 {
23  return (unsigned int)time(NULL);
24 }
25 
26 inline void SetRandomSeed(unsigned int seed)
27 {
28  srand(seed);
29 }
30 
31 // Uniform random double between 0 and 1
32 inline double RandomFloat()
33 {
34  return ((double)rand())/(((double)RAND_MAX)+1.0);
35 }
36 
37 // Uniform random double between -1 and 1
38 inline double RandomClamped()
39 {
40  return (RandomFloat()-RandomFloat());
41 }
42 
43 // Gaussian random numbers with std. dev. 0.5
44 inline double RandomGauss()
45 {
46  static int ncalls = -1;
47  static double y1;
48  static double y2;
49  ncalls++;
50  if (ncalls%2 == 0)
51  {
52  double x1,x2,w;
53  do
54  {
55  double r1 = RandomFloat();
56  double r2 = RandomFloat();
57 
58  x1 = (2.0*r1)-1.0;
59  x2 = (2.0*r2)-1.0;
60 
61  w = x1*x1 + x2*x2;
62  } while (w >= 1.0);
63  double u = sqrt(-2.0*log(w)/w);
64  y1 = u*x1*0.5;
65  y2 = u*x2*0.5;
66  return y1;
67  }
68  else
69  {
70  return y2;
71  }
72 }
73 
74 // Uniform random integer between x and y
75 inline int RandomInt(const int x,const int y)
76 {
77  return rand()%(y-x+1)+x;
78 }
79 
80 }
81 
82 }//namespace nnet
83 
84 #endif