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 :
10 : #include <time.h>
11 :
12 : #include <random>
13 :
14 : #include <formula/random.hxx>
15 : #include <rtl/instance.hxx>
16 :
17 : namespace {
18 :
19 : struct CalcFormulaRandomGenerator
20 : {
21 : std::mt19937 aRng;
22 0 : CalcFormulaRandomGenerator()
23 0 : {
24 : // initialises the state of this RNG.
25 : // should only be called once.
26 0 : bool bRepeatable = (getenv("SC_RAND_REPEATABLE") != 0);
27 0 : aRng.seed(bRepeatable ? 42 : time(NULL));
28 0 : }
29 : };
30 :
31 : class theCalcFormulaRandomGenerator : public rtl::Static<CalcFormulaRandomGenerator, theCalcFormulaRandomGenerator> {};
32 :
33 : }
34 :
35 : namespace formula
36 : {
37 :
38 : namespace rng
39 : {
40 :
41 0 : double fRandom(double a, double b)
42 : {
43 0 : std::uniform_real_distribution<double> dist(a, b);
44 0 : return dist(theCalcFormulaRandomGenerator::get().aRng);
45 : }
46 :
47 0 : sal_Int32 nRandom(sal_Int32 a, sal_Int32 b)
48 : {
49 0 : std::uniform_int_distribution<sal_Int32> dist(a, b);
50 0 : return dist(theCalcFormulaRandomGenerator::get().aRng);
51 : }
52 :
53 : } // rng
54 : } // formula
55 :
56 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|