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 :
14 : #include <boost/random.hpp>
15 :
16 : // this is nothing but a simple wrapper around
17 : // the boost random generators
18 :
19 : namespace sc
20 : {
21 : namespace rng
22 : {
23 :
24 : // underlying random number generator
25 : // boost::mt19937 implements the Mersenne twister algorithm which
26 : // is fast and has good statistical properties, it produces integers
27 : // in the range of [0, 2^32-1] internally
28 : // memory requirement: 625*sizeof(uint32_t)
29 : // http://en.wikipedia.org/wiki/Mersenne_twister
30 : #define BOOST_RNG_ALGO boost::mt19937
31 5 : BOOST_RNG_ALGO global_rng;
32 :
33 : // initialises the state of the global random number generator
34 : // should only be called once at the start of the main programme
35 : // (note, a few boost::variate_generator<> (like normal) have their
36 : // own state which would need a reset as well to guarantee identical
37 : // sequence of numbers, e.g. via myrand.distribution().reset())
38 5 : void seed(int i)
39 : {
40 5 : global_rng.seed(i);
41 5 : }
42 :
43 : // uniform [0,1) or [a,b) distribution
44 0 : double uniform()
45 : {
46 0 : static boost::uniform_01<BOOST_RNG_ALGO&> myrand(global_rng);
47 0 : return myrand();
48 : }
49 :
50 : } // namespace
51 15 : } // namespace
52 :
53 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|