LCOV - code coverage report
Current view: top level - comphelper/source/misc - random.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 11 19 57.9 %
Date: 2015-06-13 12:38:46 Functions: 3 5 60.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * Contributor(s):
      10             :  *   Copyright (C) 2012 Tino Kluge <tino.kluge@hrz.tu-chemnitz.de>
      11             :  */
      12             : 
      13             : #include <comphelper/random.hxx>
      14             : #include <rtl/instance.hxx>
      15             : #include <sal/log.hxx>
      16             : #include <assert.h>
      17             : #include <time.h>
      18             : #include <random>
      19             : #include <stdexcept>
      20             : 
      21             : // this is nothing but a simple wrapper around
      22             : // the std::random generators
      23             : 
      24             : namespace comphelper
      25             : {
      26             : namespace rng
      27             : {
      28             : 
      29             : // underlying random number generator
      30             : // std::mt19937 implements the Mersenne twister algorithm which
      31             : // is fast and has good statistical properties, it produces integers
      32             : // in the range of [0, 2^32-1] internally
      33             : // memory requirement: 625*sizeof(uint32_t)
      34             : // http://en.wikipedia.org/wiki/Mersenne_twister
      35             : #define STD_RNG_ALGO  std::mt19937
      36             : 
      37             : struct RandomNumberGenerator
      38             : {
      39             :     STD_RNG_ALGO global_rng;
      40         140 :     RandomNumberGenerator()
      41         140 :     {
      42             :         try
      43             :         {
      44         140 :             std::random_device rd;
      45             :             // initialises the state of the global random number generator
      46             :             // should only be called once.
      47             :             // (note, a few std::variate_generator<> (like normal) have their
      48             :             // own state which would need a reset as well to guarantee identical
      49             :             // sequence of numbers, e.g. via myrand.distribution().reset())
      50         140 :             global_rng.seed(rd() ^ time(nullptr));
      51             :         }
      52           0 :         catch (std::runtime_error& e)
      53             :         {
      54             :             SAL_WARN("comphelper.random", "Using std::random_device failed: " << e.what());
      55           0 :             global_rng.seed(time(nullptr));
      56             :         }
      57         140 :     }
      58             : };
      59             : 
      60             : class theRandomNumberGenerator : public rtl::Static<RandomNumberGenerator, theRandomNumberGenerator> {};
      61             : 
      62             : // uniform ints [a,b] distribution
      63       38760 : int uniform_int_distribution(int a, int b)
      64             : {
      65       38760 :     std::uniform_int_distribution<int> dist(a, b);
      66       38760 :     return dist(theRandomNumberGenerator::get().global_rng);
      67             : }
      68             : 
      69             : // uniform ints [a,b] distribution
      70           0 : unsigned int uniform_uint_distribution(unsigned int a, unsigned int b)
      71             : {
      72           0 :     std::uniform_int_distribution<unsigned int> dist(a, b);
      73           0 :     return dist(theRandomNumberGenerator::get().global_rng);
      74             : }
      75             : 
      76             : // uniform size_t [a,b] distribution
      77       20373 : size_t uniform_size_distribution(size_t a, size_t b)
      78             : {
      79       20373 :     std::uniform_int_distribution<size_t> dist(a, b);
      80       20373 :     return dist(theRandomNumberGenerator::get().global_rng);
      81             : }
      82             : 
      83             : // uniform size_t [a,b) distribution
      84           0 : double uniform_real_distribution(double a, double b)
      85             : {
      86             :     assert(a < b);
      87           0 :     std::uniform_real_distribution<double> dist(a, b);
      88           0 :     return dist(theRandomNumberGenerator::get().global_rng);
      89             : }
      90             : 
      91             : } // namespace
      92             : } // namespace
      93             : 
      94             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11