Krita Source Code Documentation
Loading...
Searching...
No Matches
KisPerStrokeRandomSource.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2017 Dmitry Kazakov <dimula73@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
8
9#include <QHash>
10#include <QMutex>
11#include <QMutexLocker>
12#include <QRandomGenerator>
13
14#include <boost/random/normal_distribution.hpp>
15#include <boost/random/taus88.hpp>
16#include <boost/random/uniform_smallint.hpp>
17
19{
20 Private(int _seed)
21 : seed(_seed)
22 {
23 boost::taus88 tempGenerator(seed);
24 generatorMax = tempGenerator.max();
25 }
26
27 Private(const Private &rhs)
28 : seed(rhs.seed),
31 {
32 }
33
34 qint64 fetchInt(const QString &key);
35
36 int seed = 0;
37 qint64 generatorMax = 0;
38 QHash<QString, qint64> valuesCache;
39 QMutex mutex;
40};
41
43 : m_d(new Private(QRandomGenerator::global()->generate()))
44{
45
46}
47
53
57
58
60{
61 QMutexLocker l(&mutex);
62
63 auto it = valuesCache.find(key);
64 if (it != valuesCache.end()) {
65 return it.value();
66 }
67
68 boost::taus88 oneTimeGenerator(seed + qHash(key));
69 const qint64 newValue = oneTimeGenerator();
70
71 valuesCache.insert(key, newValue);
72
73 return newValue;
74}
75
76int KisPerStrokeRandomSource::generate(const QString &key, int min, int max) const
77{
78 return min + m_d->fetchInt(key) % (max - min);
79}
80
81qreal KisPerStrokeRandomSource::generateNormalized(const QString &key) const
82{
83 return qreal(m_d->fetchInt(key)) / m_d->generatorMax;
84}
85
uint qHash(const KoInputDevice &key)
const QScopedPointer< Private > m_d
qreal generateNormalized(const QString &key) const
int generate(const QString &key, int min, int max) const