CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
Loading...
Searching...
No Matches
CLHEP::RandEngineBuilder< int > Struct Template Reference

Static Public Member Functions

static unsigned int thirtyTwoRandomBits (long &seq)
 

Detailed Description

template<int>
struct CLHEP::RandEngineBuilder< int >

Definition at line 245 of file RandEngine.cc.

Member Function Documentation

◆ thirtyTwoRandomBits()

template<int >
static unsigned int CLHEP::RandEngineBuilder< int >::thirtyTwoRandomBits ( long &  seq)
inlinestatic

Definition at line 246 of file RandEngine.cc.

246 {
247
248 static bool prepared = false;
249 static unsigned int iT;
250 static unsigned int iK;
251 static unsigned int iS;
252 static int iN;
253 static double fS;
254 static double fT;
255
256 if ( (RAND_MAX >> 31) > 0 )
257 {
258 // Here, we know that integer arithmetic is 64 bits.
259 if ( !prepared ) {
260 iS = (unsigned long)RAND_MAX + 1;
261 iK = 1;
262// int StoK = S;
263 int StoK = iS;
264 // The two statements below are equivalent, but some compilers
265 // are getting too smart and complain about the first statement.
266 //if ( (RAND_MAX >> 32) == 0) {
267 if( (unsigned long) (RAND_MAX) <= (( (1uL) << 31 ) - 1 ) ) {
268 iK = 2;
269// StoK = S*S;
270 StoK = iS*iS;
271 }
272 int m;
273 for ( m = 0; m < 64; ++m ) {
274 StoK >>= 1;
275 if (StoK == 0) break;
276 }
277 iT = 1 << m;
278 prepared = true;
279 }
280 int v = 0;
281 do {
282 for ( int i = 0; i < iK; ++i ) {
283 v = iS*v+rand(); ++seq;
284 }
285 } while (v < iT);
286 return v & 0xFFFFFFFF;
287
288 }
289
290 else if ( (RAND_MAX >> 26) == 0 )
291 {
292 // Here, we know it is safe to work in terms of doubles without loss
293 // of precision, but we have no idea how many randoms we will need to
294 // generate 32 bits.
295 if ( !prepared ) {
296 fS = (unsigned long)RAND_MAX + 1;
297 double twoTo32 = std::ldexp(1.0,32);
298 double StoK = fS;
299 for ( iK = 1; StoK < twoTo32; StoK *= fS, iK++ ) { }
300 int m;
301 fT = 1.0;
302 for ( m = 0; m < 64; ++m ) {
303 StoK *= .5;
304 if (StoK < 1.0) break;
305 fT *= 2.0;
306 }
307 prepared = true;
308 }
309 double v = 0;
310 do {
311 for ( int i = 0; i < iK; ++i ) {
312 v = fS*v+rand(); ++seq;
313 }
314 } while (v < fT);
315 return ((unsigned int)v) & 0xFFFFFFFF;
316
317 }
318 else
319 {
320 // Here, we know that 16 random bits are available from each of
321 // two random numbers.
322 if ( !prepared ) {
323 iS = (unsigned long)RAND_MAX + 1;
324 int SshiftN = iS;
325 for (iN = 0; SshiftN > 1; SshiftN >>= 1, iN++) { }
326 iN -= 17;
327 prepared = true;
328 }
329 unsigned int x1, x2;
330 do { x1 = rand(); ++seq;} while (x1 < (1<<16) );
331 do { x2 = rand(); ++seq;} while (x2 < (1<<16) );
332 return x1 | (x2 << 16);
333 }
334
335 }

Referenced by CLHEP::RandEngine::flat(), and CLHEP::RandEngine::operator unsigned int().


The documentation for this struct was generated from the following file: