-
Notifications
You must be signed in to change notification settings - Fork 1
/
randomgenerator.cpp
87 lines (72 loc) · 2.21 KB
/
randomgenerator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright 2015 Klimov Viktor, https://github.com/Vitek1425/
#include "randomgenerator.h"
#include <QDebug>
std::default_random_engine RandomGenerator::m_generator;
RandomGenerator::RandomGenerator() : RandomGenerator(10, 20, "(function(x) { return (x-10)/10; })")
{
}
RandomGenerator::RandomGenerator(qreal leftBorder, qreal rightBorder, const QString &distributionFunction):
m_leftBorder(leftBorder),
m_rightBorder(rightBorder),
m_distributionFunction(distributionFunction)
{
m_fun = m_engine.evaluate(m_distributionFunction);
}
qreal RandomGenerator::getRandom()
{
std::uniform_real_distribution<double> distribution(0,1);
double y = distribution(m_generator);
if(y == 0) return m_leftBorder;
if(y == 1) return m_rightBorder;
return computeInverseValue(y);
}
qreal RandomGenerator::getLeftBorder() const
{
return m_leftBorder;
}
void RandomGenerator::setLeftBorder(const qreal &leftBorder)
{
m_leftBorder = leftBorder;
}
qreal RandomGenerator::getRightBorder() const
{
return m_rightBorder;
}
void RandomGenerator::setRightBorder(const qreal &rightBorder)
{
m_rightBorder = rightBorder;
}
QString RandomGenerator::getDistributionFunction() const
{
return m_distributionFunction;
}
void RandomGenerator::setDistributionFunction(const QString &distributionFunction)
{
m_distributionFunction = distributionFunction;
updateFunction();
}
qreal RandomGenerator::computeInverseValue(qreal y)
{
//поиск обратного значения методом бисекции
double tempValue = 0;
double precision = 0.001;
double leftBorder = m_leftBorder;
double rightBorder = m_rightBorder;
while(rightBorder - leftBorder > precision)
{
tempValue = (rightBorder + leftBorder)/2;
QJSValueList firstValue;
QJSValueList secondValue;
firstValue << rightBorder;
secondValue << tempValue;
if (((m_fun.call(firstValue).toNumber() - y) * (m_fun.call(secondValue).toNumber() - y)< 0))
leftBorder = tempValue;
else
rightBorder = tempValue;
}
return (rightBorder + leftBorder)/2;
}
void RandomGenerator::updateFunction()
{
m_fun = m_engine.evaluate(m_distributionFunction);
}