Bump version to 4.3-4
[LibreOffice.git] / comphelper / source / misc / random.cxx
bloba97608649bbffcee1814a4fda560258a9fdcfaa6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * Contributor(s):
10 * Copyright (C) 2012 Tino Kluge <tino.kluge@hrz.tu-chemnitz.de>
13 #include <boost/random.hpp>
15 #include <comphelper/random.hxx>
17 // this is nothing but a simple wrapper around
18 // the boost random generators
20 namespace comphelper
22 namespace rng
25 // underlying random number generator
26 // boost::mt19937 implements the Mersenne twister algorithm which
27 // is fast and has good statistical properties, it produces integers
28 // in the range of [0, 2^32-1] internally
29 // memory requirement: 625*sizeof(uint32_t)
30 // http://en.wikipedia.org/wiki/Mersenne_twister
31 #define BOOST_RNG_ALGO boost::mt19937
32 BOOST_RNG_ALGO global_rng;
34 // initialises the state of the global random number generator
35 // should only be called once at the start of the main programme
36 // (note, a few boost::variate_generator<> (like normal) have their
37 // own state which would need a reset as well to guarantee identical
38 // sequence of numbers, e.g. via myrand.distribution().reset())
39 void seed(int i)
41 global_rng.seed(i);
44 // uniform [0,1) or [a,b) distribution
45 double uniform()
47 static boost::uniform_01<BOOST_RNG_ALGO&> myrand(global_rng);
48 return myrand();
51 } // namespace
52 } // namespace
54 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */