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