File: testBug73093.cc

package info (click to toggle)
clhep 2.1.4.1%2Bdfsg-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 10,012 kB
  • sloc: cpp: 50,094; sh: 6,694; makefile: 2,694; perl: 28
file content (96 lines) | stat: -rw-r--r-- 2,238 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// ----------------------------------------------------------------------
//
// testBug73093 -- Test of CLHEP::Ranlux64Engine with 64 bit seeds
//
// Frank Winklmeier  2010-09-24 
// L. Garren	     2010-10-21	rewritten for test suite
// 
// ----------------------------------------------------------------------

#include <iostream>
#include <cmath>
#include <stdlib.h>

#include "CLHEP/Random/Ranlux64Engine.h"

int valid_range( )
{
    std::ofstream output("testBug73093.cout");  

    int bad = 0;
    long seed;
    long mult=-235421;
    // use several seeds
    for( int il=0; il<100; ++il ) {
	if(  sizeof(long) > 4 ) {
            // using atol so 32bit compilers won't complain
            seed =       atol("9899876543210000");
	    mult = mult + atol("120034020050070");
	} else {
            seed =       987654321;
	    mult = mult + 12003400;
	}
	seed += il*mult;

	CLHEP::Ranlux64Engine rng;
	const long N = 20;

	rng.setSeed(seed, /*lux*/ 1);
	output <<  std::endl;
	output << "sizeof(long) = " << sizeof(long) << std::endl;
	output << "Generating " << N << " random numbers with seed " << seed << std::endl;
	output << "Using seed " << seed <<  std::endl;

	double sum(0);
	for (long i=0; i<N; ++i) {
	  double r = rng.flat();
	  if( std::abs(r) > 1.0 ) ++bad;
	  output << r << std::endl;
	  sum += r;
	}  

	output << "Sum: " << sum << std::endl;
	output << "Average: " << sum / N << std::endl;
    }
    
    return bad;
}

int check_sequence()
{
    // if the seed is less than 32bits long on a 64bit machine,  the random
    // number sequence should be the same as the sequence on a 32bit machine
    std::ofstream output("testBug73093.seq");  
    int bad = 0;
    long seed;
    long mult=-235421;
    // use several seeds
    for( int il=0; il<50; ++il ) {
        seed = 97654321;
	seed += il*mult;

	CLHEP::Ranlux64Engine rng;
	const long N = 20;

	rng.setSeed(seed, /*lux*/ 1);

	double sum(0);
	for (long i=0; i<N; ++i) {
	  double r = rng.flat();
	  if( std::abs(r) > 1.0 ) ++bad;
	  output << "[" << il << "][" << i << "] = " << r << ";" << std::endl;
	  sum += r;
	}  
    }
    return bad;
}

int main()
{

    int bad = 0;
    bad += valid_range( );
    bad += check_sequence( );
    
    return bad;
}