1 /*
2  * Copyright 2009-2020 The VOTCA Development Team (http://www.votca.org)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 #define BOOST_TEST_MAIN
19 
20 #define BOOST_TEST_MODULE random2_test
21 
22 // Standard includes
23 #include <iostream>
24 #include <string>
25 
26 // Third party includes
27 #include <boost/test/tools/floating_point_comparison.hpp>
28 #include <boost/test/unit_test.hpp>
29 
30 // Local VOTCA includes
31 #include "votca/tools/random.h"
32 
33 using namespace std;
34 using namespace votca::tools;
35 
36 BOOST_AUTO_TEST_SUITE(random2_test)
37 
BOOST_AUTO_TEST_CASE(random_int_test)38 BOOST_AUTO_TEST_CASE(random_int_test) {
39   Random random;
40   votca::Index seed = 1;
41   random.init(seed);
42   random.setMaxInt(50);
43   std::vector<votca::Index> results;
44   votca::Index number = 1e5;
45   results.reserve(number);
46   for (votca::Index i = 0; i < number; i++) {
47     results.push_back(random.rand_uniform_int());
48   }
49 
50   // average should be close to 25
51   double average = std::accumulate(results.begin(), results.end(), 0);
52   average /= double(number);
53 
54   BOOST_CHECK_CLOSE(average, 25, 1.0);
55 }
56 
BOOST_AUTO_TEST_CASE(random_double_test)57 BOOST_AUTO_TEST_CASE(random_double_test) {
58   Random random;
59   votca::Index seed = 1;
60   random.init(seed);
61   std::vector<double> results;
62   votca::Index number = 1e5;
63   results.reserve(number);
64   for (votca::Index i = 0; i < number; i++) {
65     results.push_back(random.rand_uniform());
66   }
67 
68   // average should be close to 0.5
69   double average = std::accumulate(results.begin(), results.end(), 0.0);
70   average /= double(number);
71 
72   BOOST_CHECK_CLOSE(average, 0.5, 1.0);
73 }
74 
75 BOOST_AUTO_TEST_SUITE_END()
76