Geant4  10.00.p03
G4MTRandGauss.cc
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 //
27 // $Id:$
28 //
29 #ifdef G4MULTITHREADED
30 
31 #include <cmath> // for log()
32 
33 #include "G4MTRandGauss.hh"
34 
35 // Initialisation of static data
38 
40 {
41  if ( deleteEngine ) delete localEngine;
42 }
43 
45 {
46  return fire( defaultMean, defaultStdDev );
47 }
48 
50 {
51  return fire( mean, stdDev );
52 }
53 
55 {
56  // Gaussian random numbers are generated two at the time, so every other
57  // time this is called we just return a number generated the time before.
58 
59  if ( getFlag() ) {
60  setFlag(false);
61  G4double x = getVal();
62  return x;
63  // return getVal();
64  }
65 
66  G4double r;
67  G4double v1,v2,fac,val;
68  CLHEP::HepRandomEngine* anEngine = G4MTHepRandom::getTheEngine();
69 
70  do {
71  v1 = 2.0 * anEngine->flat() - 1.0;
72  v2 = 2.0 * anEngine->flat() - 1.0;
73  r = v1*v1 + v2*v2;
74  } while ( r > 1.0 );
75 
76  fac = std::sqrt(-2.0*std::log(r)/r);
77  val = v1*fac;
78  setVal(val);
79  setFlag(true);
80  return v2*fac;
81 }
82 
83 void G4MTRandGauss::shootArray( const G4int size, G4double* vect,
84  G4double mean, G4double stdDev )
85 {
86  for (G4int i=0; i<size; ++i)
87  vect[i] = shoot(mean,stdDev);
88 }
89 
90 G4double G4MTRandGauss::shoot( CLHEP::HepRandomEngine* anEngine )
91 {
92  // Gaussian random numbers are generated two at the time, so every other
93  // time this is called we just return a number generated the time before.
94 
95  if ( getFlag() ) {
96  setFlag(false);
97  return getVal();
98  }
99 
100  G4double r;
101  G4double v1,v2,fac,val;
102 
103  do {
104  v1 = 2.0 * anEngine->flat() - 1.0;
105  v2 = 2.0 * anEngine->flat() - 1.0;
106  r = v1*v1 + v2*v2;
107  } while ( r > 1.0 );
108 
109  fac = std::sqrt( -2.0*std::log(r)/r);
110  val = v1*fac;
111  setVal(val);
112  setFlag(true);
113  return v2*fac;
114 }
115 
116 void G4MTRandGauss::shootArray( CLHEP::HepRandomEngine* anEngine,
117  const G4int size, G4double* vect,
118  G4double mean, G4double stdDev )
119 {
120  for (G4int i=0; i<size; ++i)
121  vect[i] = shoot(anEngine,mean,stdDev);
122 }
123 
125 {
126  // Gaussian random numbers are generated two at the time, so every other
127  // time this is called we just return a number generated the time before.
128 
129  if ( set ) {
130  set = false;
131  return nextGauss;
132  }
133 
134  G4double r;
135  G4double v1,v2,fac,val;
136 
137  do {
138  v1 = 2.0 * localEngine->flat() - 1.0;
139  v2 = 2.0 * localEngine->flat() - 1.0;
140  r = v1*v1 + v2*v2;
141  } while ( r > 1.0 );
142 
143  fac = std::sqrt(-2.0*std::log(r)/r);
144  val = v1*fac;
145  nextGauss = val;
146  set = true;
147  return v2*fac;
148 }
149 
150 void G4MTRandGauss::fireArray( const G4int size, G4double* vect)
151 {
152  for (G4int i=0; i<size; ++i)
153  vect[i] = fire( defaultMean, defaultStdDev );
154 }
155 
156 void G4MTRandGauss::fireArray( const G4int size, G4double* vect,
157  G4double mean, G4double stdDev )
158 {
159  for (G4int i=0; i<size; ++i)
160  vect[i] = fire( mean, stdDev );
161 }
162 
163 #endif
G4double defaultStdDev
static CLHEP::HepRandomEngine * getTheEngine()
virtual G4double operator()()
static const G4double fac
G4double nextGauss
static void shootArray(const G4int size, G4double *vect, G4double mean=0.0, G4double stdDev=1.0)
G4double defaultMean
G4double fire()
#define G4ThreadLocal
Definition: tls.hh:52
int G4int
Definition: G4Types.hh:78
static G4ThreadLocal G4double nextGauss_st
G4double normal()
bool G4bool
Definition: G4Types.hh:79
static G4bool getFlag()
virtual ~G4MTRandGauss()
void fireArray(const G4int size, G4double *vect)
static G4double shoot()
double G4double
Definition: G4Types.hh:76
static G4double getVal()
static void setFlag(G4bool val)
CLHEP::HepRandomEngine * localEngine
static void setVal(G4double nextVal)
static G4ThreadLocal G4bool set_st