Geant4  10.03
RandFlat.icc
Go to the documentation of this file.
1 // $Id:$
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 
22 namespace CLHEP {
23 
24 inline RandFlat::RandFlat(HepRandomEngine & anEngine)
25 : HepRandom(), firstUnusedBit(0), localEngine(&anEngine, do_nothing_deleter()),
26  defaultWidth(1.0), defaultA(0.0), defaultB(1.0) {}
27 
28 inline RandFlat::RandFlat(HepRandomEngine & anEngine, double width )
29 : HepRandom(), firstUnusedBit(0), localEngine(&anEngine, do_nothing_deleter()),
30  defaultWidth(width), defaultA(0.0), defaultB(width) {}
31 
32 inline 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 
37 inline RandFlat::RandFlat(HepRandomEngine * anEngine)
38 : HepRandom(), firstUnusedBit(0), localEngine(anEngine),
39  defaultWidth(1.0), defaultA(0.0), defaultB(1.0) {}
40 
41 inline RandFlat::RandFlat(HepRandomEngine * anEngine, double width )
42 : HepRandom(), firstUnusedBit(0), localEngine(anEngine),
43  defaultWidth(width), defaultA(0.0), defaultB(width) {}
44 
45 inline RandFlat::RandFlat(HepRandomEngine * anEngine, double a,
46  double b )
47 : HepRandom(), firstUnusedBit(0), localEngine(anEngine),
48  defaultWidth(b-a), defaultA(a), defaultB(b) {}
49 
50 inline double RandFlat::shoot(double a, double b) {
51  return (b-a)* shoot() + a;
52 }
53 
54 inline double RandFlat::shoot(double width) {
55  return width * shoot();
56 }
57 
58 inline long RandFlat::shootInt(long n) {
59  return long(shoot()*double(n));
60 }
61 
62 inline long RandFlat::shootInt(long a1, long n) {
63  return long(shoot()*double(n-a1)) + a1;
64 }
65 
66 inline 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 
72 inline 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 
82 inline double RandFlat::shoot(HepRandomEngine* anEngine) {
83  return anEngine->flat();
84 }
85 
86 
87 inline double RandFlat::shoot(HepRandomEngine* anEngine,
88  double a, double b) {
89  return (b-a)* anEngine->flat() + a;
90 }
91 
92 inline double RandFlat::shoot(HepRandomEngine* anEngine,
93  double width) {
94  return width * anEngine->flat();
95 }
96 
97 inline long RandFlat::shootInt(HepRandomEngine* anEngine,
98  long n) {
99  return long(anEngine->flat()*double(n));
100 }
101 
102 inline long RandFlat::shootInt(HepRandomEngine* anEngine,
103  long a1, long n) {
104  return long(double(n-a1)*anEngine->flat()) + a1;
105 }
106 
107 inline void RandFlat::shootArray(HepRandomEngine* anEngine,
108  const int size, double* vect) {
109  anEngine->flatArray(size,vect);
110 }
111 
112 inline 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 
118 inline 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 
128 inline double RandFlat::fire() {
129  return (defaultB-defaultA)*localEngine->flat()+defaultA;
130 }
131 
132 inline double RandFlat::fire(double a, double b) {
133  return (b-a)* localEngine->flat() + a;
134 }
135 
136 inline double RandFlat::fire(double width) {
137  return width * localEngine->flat();
138 }
139 
140 inline long RandFlat::fireInt(long n) {
141  return long(localEngine->flat()*double(n));
142 }
143 
144 inline long RandFlat::fireInt(long a1, long n) {
145  return long(localEngine->flat()*double(n-a1)) + a1;
146 }
147 
148 inline 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 
154 inline 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