Geant4  10.02.p01
G4MTRandFlat.icc
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 inline G4MTRandFlat::
30 G4MTRandFlat(CLHEP::HepRandomEngine & anEngine)
31 : firstUnusedBit(0), localEngine(&anEngine), deleteEngine(false),
32  defaultA(0.0), defaultB(1.0) {}
33 
34 inline G4MTRandFlat::
35 G4MTRandFlat(CLHEP::HepRandomEngine & anEngine, G4double width )
36 : firstUnusedBit(0), localEngine(&anEngine), deleteEngine(false),
37  defaultA(0.0), defaultB(width) {}
38 
39 inline G4MTRandFlat::
40 G4MTRandFlat(CLHEP::HepRandomEngine & anEngine, G4double a, G4double b )
41 : firstUnusedBit(0), localEngine(&anEngine), deleteEngine(false),
42  defaultA(a), defaultB(b) {}
43 
44 inline G4MTRandFlat::
45 G4MTRandFlat(CLHEP::HepRandomEngine * anEngine)
46 : firstUnusedBit(0), localEngine(anEngine), deleteEngine(true),
47  defaultA(0.0), defaultB(1.0) {}
48 
49 inline G4MTRandFlat::
50 G4MTRandFlat(CLHEP::HepRandomEngine * anEngine, G4double width )
51 : firstUnusedBit(0), localEngine(anEngine), deleteEngine(true),
52  defaultA(0.0), defaultB(width) {}
53 
54 inline G4MTRandFlat::
55 G4MTRandFlat(CLHEP::HepRandomEngine * anEngine, G4double a, G4double b )
56 : firstUnusedBit(0), localEngine(anEngine), deleteEngine(true),
57  defaultA(a), defaultB(b) {}
58 
59 inline G4double G4MTRandFlat::shoot(G4double a, G4double b)
60 {
61  return (b-a)* shoot() + a;
62 }
63 
64 inline G4double G4MTRandFlat::shoot(G4double width)
65 {
66  return width * shoot();
67 }
68 
69 inline G4long G4MTRandFlat::shootInt(G4long n)
70 {
71  return G4long(shoot()*G4double(n));
72 }
73 
74 inline G4long G4MTRandFlat::shootInt(G4long mLong, G4long nLong)
75 {
76  return G4long(shoot()*G4double(nLong-mLong)) + mLong;
77 }
78 
79 inline void G4MTRandFlat::shootBits()
80 {
81  const G4double factor= 2.0*MSB; // this should fit into a double!
82  staticFirstUnusedBit= MSB;
83  staticRandomInt= (unsigned long)(factor*shoot());
84 }
85 
86 inline G4int G4MTRandFlat::shootBit()
87 {
88  if (staticFirstUnusedBit==0)
89  shootBits();
90  unsigned long temp= staticFirstUnusedBit&staticRandomInt;
91  staticFirstUnusedBit>>= 1;
92  return temp!=0;
93 }
94 
95 //---------------------
96 
97 inline G4double G4MTRandFlat::shoot(CLHEP::HepRandomEngine* anEngine)
98 {
99  return anEngine->flat();
100 }
101 
102 
103 inline G4double G4MTRandFlat::shoot(CLHEP::HepRandomEngine* anEngine,
104  G4double a, G4double b)
105 {
106  return (b-a)* anEngine->flat() + a;
107 }
108 
109 inline G4double G4MTRandFlat::shoot(CLHEP::HepRandomEngine* anEngine,
110  G4double width)
111 {
112  return width * anEngine->flat();
113 }
114 
115 inline G4long G4MTRandFlat::shootInt(CLHEP::HepRandomEngine* anEngine,
116  G4long n)
117 {
118  return G4long(anEngine->flat()*G4double(n));
119 }
120 
121 inline G4long G4MTRandFlat::shootInt(CLHEP::HepRandomEngine* anEngine,
122  G4long mparam, G4long n)
123 {
124  return G4long(G4double(n-mparam)*anEngine->flat()) + mparam;
125 }
126 
127 inline void G4MTRandFlat::shootArray(CLHEP::HepRandomEngine* anEngine,
128  const G4int size, G4double* vect)
129 {
130  anEngine->flatArray(size,vect);
131 }
132 
133 inline void G4MTRandFlat::shootBits(CLHEP::HepRandomEngine* engine)
134 {
135  const G4double factor= 2.0*MSB; // this should fit into a double!
136  staticFirstUnusedBit= MSB;
137  staticRandomInt= (unsigned long)(factor*shoot(engine));
138 }
139 
140 inline G4int G4MTRandFlat::shootBit(CLHEP::HepRandomEngine* engine)
141 {
142  if (staticFirstUnusedBit==0)
143  shootBits(engine);
144  unsigned long temp= staticFirstUnusedBit&staticRandomInt;
145  staticFirstUnusedBit>>= 1;
146  return temp!=0;
147 }
148 
149 //---------------------
150 
151 inline G4double G4MTRandFlat::fire()
152 {
153  return (defaultB-defaultA)*localEngine->flat()+defaultA;
154 }
155 
156 inline G4double G4MTRandFlat::fire(G4double a, G4double b)
157 {
158  return (b-a)* localEngine->flat() + a;
159 }
160 
161 inline G4double G4MTRandFlat::fire(G4double width)
162 {
163  return width * localEngine->flat();
164 }
165 
166 inline G4long G4MTRandFlat::fireInt(G4long n)
167 {
168  return G4long(localEngine->flat()*G4double(n));
169 }
170 
171 inline G4long G4MTRandFlat::fireInt(G4long mparam, G4long n)
172 {
173  return G4long(localEngine->flat()*G4double(n-mparam)) + mparam;
174 }
175 
176 inline void G4MTRandFlat::fireBits()
177 {
178  const G4double factor= 2.0*MSB; // this should fit into a double!
179  firstUnusedBit= MSB;
180  randomInt= (unsigned long)(factor*localEngine->flat());
181 }
182 
183 inline G4int G4MTRandFlat::fireBit()
184 {
185  if (firstUnusedBit==0)
186  fireBits();
187  unsigned long temp= firstUnusedBit&randomInt;
188  firstUnusedBit>>= 1;
189  return temp!=0;
190 }