Geant4-11
RandFlat.icc
Go to the documentation of this file.
1// -*- C++ -*-
2//
3// -----------------------------------------------------------------------
4// HEP Random
5// --- RandFlat ---
6// inlined functions implementation file
7// -----------------------------------------------------------------------
8// This file is part of Geant4 (simulation toolkit for HEP).
9
10// =======================================================================
11// Gabriele Cosmo - Created: 5th September 1995
12// Peter Urban - ShootBit() and related stuff added: 5th Sep 1996
13// Gabriele Cosmo - Additional methods to fill arrays specifying
14// boundaries: 24th Jul 1997
15// - Fixed bug in shootInt(m,n): 25th Sep 1997
16// J.Marraffino - Added default arguments as attributes: 16th Feb 1998
17// M.Fischler - Corrected initialization of deleteEngine which should
18// be true for all constructors taking HepRandomEngine*.
19// =======================================================================
20
21namespace CLHEP {
22
23inline RandFlat::RandFlat(HepRandomEngine & anEngine)
24: HepRandom(), firstUnusedBit(0), localEngine(&anEngine, do_nothing_deleter()),
25 defaultWidth(1.0), defaultA(0.0), defaultB(1.0) {}
26
27inline RandFlat::RandFlat(HepRandomEngine & anEngine, double width )
28: HepRandom(), firstUnusedBit(0), localEngine(&anEngine, do_nothing_deleter()),
29 defaultWidth(width), defaultA(0.0), defaultB(width) {}
30
31inline RandFlat::RandFlat(HepRandomEngine & anEngine, double a,
32 double b )
33: HepRandom(), firstUnusedBit(0), localEngine(&anEngine, do_nothing_deleter()),
34 defaultWidth(b-a), defaultA(a), defaultB(b) {}
35
36inline RandFlat::RandFlat(HepRandomEngine * anEngine)
37: HepRandom(), firstUnusedBit(0), localEngine(anEngine),
38 defaultWidth(1.0), defaultA(0.0), defaultB(1.0) {}
39
40inline RandFlat::RandFlat(HepRandomEngine * anEngine, double width )
41: HepRandom(), firstUnusedBit(0), localEngine(anEngine),
42 defaultWidth(width), defaultA(0.0), defaultB(width) {}
43
44inline RandFlat::RandFlat(HepRandomEngine * anEngine, double a,
45 double b )
46: HepRandom(), firstUnusedBit(0), localEngine(anEngine),
47 defaultWidth(b-a), defaultA(a), defaultB(b) {}
48
49inline double RandFlat::shoot(double a, double b) {
50 return (b-a)* shoot() + a;
51}
52
53inline double RandFlat::shoot(double width) {
54 return width * shoot();
55}
56
57inline long RandFlat::shootInt(long n) {
58 return long(shoot()*double(n));
59}
60
61inline long RandFlat::shootInt(long a1, long n) {
62 return long(shoot()*double(n-a1)) + a1;
63}
64
65inline void RandFlat::shootBits() {
66 const double factor= 2.0*MSB; // this should fit into a double!
67 staticFirstUnusedBit= MSB;
68 staticRandomInt= (unsigned long)(factor*shoot());
69}
70
71inline int RandFlat::shootBit() {
72 if (staticFirstUnusedBit==0)
73 shootBits();
74 unsigned long temp= staticFirstUnusedBit&staticRandomInt;
75 staticFirstUnusedBit>>= 1;
76 return temp!=0;
77}
78
79//---------------------
80
81inline double RandFlat::shoot(HepRandomEngine* anEngine) {
82 return anEngine->flat();
83}
84
85
86inline double RandFlat::shoot(HepRandomEngine* anEngine,
87 double a, double b) {
88 return (b-a)* anEngine->flat() + a;
89}
90
91inline double RandFlat::shoot(HepRandomEngine* anEngine,
92 double width) {
93 return width * anEngine->flat();
94}
95
96inline long RandFlat::shootInt(HepRandomEngine* anEngine,
97 long n) {
98 return long(anEngine->flat()*double(n));
99}
100
101inline long RandFlat::shootInt(HepRandomEngine* anEngine,
102 long a1, long n) {
103 return long(double(n-a1)*anEngine->flat()) + a1;
104}
105
106inline void RandFlat::shootArray(HepRandomEngine* anEngine,
107 const int size, double* vect) {
108 anEngine->flatArray(size,vect);
109}
110
111inline void RandFlat::shootBits(HepRandomEngine* engine) {
112 const double factor= 2.0*MSB; // this should fit into a double!
113 staticFirstUnusedBit= MSB;
114 staticRandomInt= (unsigned long)(factor*shoot(engine));
115}
116
117inline int RandFlat::shootBit(HepRandomEngine* engine) {
118 if (staticFirstUnusedBit==0)
119 shootBits(engine);
120 unsigned long temp= staticFirstUnusedBit&staticRandomInt;
121 staticFirstUnusedBit>>= 1;
122 return temp!=0;
123}
124
125//---------------------
126
127inline double RandFlat::fire() {
128 return (defaultB-defaultA)*localEngine->flat()+defaultA;
129}
130
131inline double RandFlat::fire(double a, double b) {
132 return (b-a)* localEngine->flat() + a;
133}
134
135inline double RandFlat::fire(double width) {
136 return width * localEngine->flat();
137}
138
139inline long RandFlat::fireInt(long n) {
140 return long(localEngine->flat()*double(n));
141}
142
143inline long RandFlat::fireInt(long a1, long n) {
144 return long(localEngine->flat()*double(n-a1)) + a1;
145}
146
147inline void RandFlat::fireBits() {
148 const double factor= 2.0*MSB; // this should fit into a double!
149 firstUnusedBit= MSB;
150 randomInt= (unsigned long)(factor*localEngine->flat());
151}
152
153inline int RandFlat::fireBit() {
154 if (firstUnusedBit==0)
155 fireBits();
156 unsigned long temp= firstUnusedBit&randomInt;
157 firstUnusedBit>>= 1;
158 return temp!=0;
159}
160
161} // namespace CLHEP