HyperDbg Debugger
Loading...
Searching...
No Matches
gaussian-rng.cpp File Reference
#include "pch.h"

Functions

double Median (vector< double > Cases)
 get the median of a vector
 
template<typename T >
Average (const vector< T > &vec)
 get the average of a vector
 
template<typename T >
CalculateStandardDeviation (const std::vector< T > &v)
 get the standard deviation of elements
 
double MedianAbsoluteDeviationTest (vector< double > Data)
 get the Median Absolute Deviation (MAD) Test
 
double Randn (double mu, double sigma)
 random generator based on calculations
 
VOID GuassianGenerateRandom (vector< double > Data, UINT64 *AverageOfData, UINT64 *StandardDeviationOfData, UINT64 *MedianOfData)
 Calculate and generate random gaussian number.
 
VOID TestGaussianFromFile ()
 A simple test for the data based on pre-defined numbers in a file.
 

Function Documentation

◆ Average()

template<typename T >
T Average ( const vector< T > & vec)

get the average of a vector

Template Parameters
Ttype of vector
Parameters
vecall the elements
Returns
T the average of elements
54{
55 size_t Sz;
56 T Mean;
57 Sz = vec.size();
58 if (Sz == 1)
59 return 0.0;
60
61 //
62 // Calculate the mean
63 //
64 Mean = std::accumulate(vec.begin(), vec.end(), 0.0) / Sz;
65
66 return Mean;
67}

◆ CalculateStandardDeviation()

template<typename T >
T CalculateStandardDeviation ( const std::vector< T > & v)

get the standard deviation of elements

Template Parameters
Ttype of vector
Parameters
vall the elements
Returns
T the standard deviation of elements
79{
80 double Sum, Mean, SqSum, Stdev;
81
82 Sum = std::accumulate(v.begin(), v.end(), 0.0);
83 Mean = Sum / v.size();
84
85 SqSum = std::inner_product(v.begin(), v.end(), v.begin(), 0.0);
86 Stdev = std::sqrt(SqSum / v.size() - Mean * Mean);
87 return Stdev;
88}

◆ GuassianGenerateRandom()

VOID GuassianGenerateRandom ( vector< double > Data,
UINT64 * AverageOfData,
UINT64 * StandardDeviationOfData,
UINT64 * MedianOfData )

Calculate and generate random gaussian number.

Parameters
Data
AverageOfData
StandardDeviationOfData
MedianOfData
159{
160 vector<double> FinalData;
161 int CountOfOutliers = 0;
162 double Medians;
163 double Mad;
164 double StandardDeviation;
165 double DataAverage;
166 double DataMedian;
167
168 vector<double> OriginalData = Data;
169 vector<double> ChangableData = std::move(Data);
170
171 Mad = MedianAbsoluteDeviationTest(ChangableData);
172 Medians = Median(OriginalData);
173
174 for (auto item : OriginalData)
175 {
176 if (item > (3 * Mad) + Medians || item < -(3 * Mad) + Medians)
177 {
178 CountOfOutliers++;
179 }
180 else
181 {
182 FinalData.push_back(item);
183 }
184 }
185
186 StandardDeviation = CalculateStandardDeviation(FinalData);
187 DataAverage = Average(FinalData);
188 DataMedian = Median(FinalData);
189
190 //
191 // Set the values to return
192 //
193 *AverageOfData = (UINT64)DataAverage;
194
195 //
196 // We add 5 to the standard deviation because this value might be
197 // 0 or 1 so we need more variance
198 //
199 *StandardDeviationOfData = (UINT64)StandardDeviation + 5;
200 *MedianOfData = (UINT64)DataMedian;
201
202 //
203 // ShowMessages("variance : %f\n", StandardDeviation);
204 // ShowMessages("mean : %f\n", DataAverage);
205 // ShowMessages("count of outliers : %d\n", CountOfOutliers);
206 //
207 //
208 // for (int i = 0; i < 10000; i++)
209 // {
210 // ShowMessages("final Random Time Stamp : %d\n", (int) Randn(DataAverage,
211 // StandardDeviation));
212 // _getch();
213 // }
214 //
215}
unsigned __int64 UINT64
Definition BasicTypes.h:21
T CalculateStandardDeviation(const std::vector< T > &v)
get the standard deviation of elements
Definition gaussian-rng.cpp:78
double Median(vector< double > Cases)
get the median of a vector
Definition gaussian-rng.cpp:22
double MedianAbsoluteDeviationTest(vector< double > Data)
get the Median Absolute Deviation (MAD) Test
Definition gaussian-rng.cpp:97
T Average(const vector< T > &vec)
get the average of a vector
Definition gaussian-rng.cpp:53
Start of Optional Data
Definition script_buffer.hex.txt:8

◆ Median()

double Median ( vector< double > Cases)

get the median of a vector

Parameters
Casesall the elements
Returns
double median of elements
23{
24 size_t Size = Cases.size();
25
26 if (Size == 0)
27 {
28 return 0; // Undefined, really
29 }
30 else
31 {
32 sort(Cases.begin(), Cases.end());
33 if (Size % 2 == 0)
34 {
35 return (Cases[Size / 2 - 1] + Cases[Size / 2]) / 2;
36 }
37 else
38 {
39 return Cases[Size / 2];
40 }
41 }
42}

◆ MedianAbsoluteDeviationTest()

double MedianAbsoluteDeviationTest ( vector< double > Data)

get the Median Absolute Deviation (MAD) Test

Parameters
Dataall the elements
Returns
double result of MAD test
98{
99 double MedianData;
100 double Mad;
101
102 MedianData = Median(Data);
103
104 for (int i = 0; i < Data.size(); i++)
105 {
106 Data[i] = abs(Data[i] - MedianData);
107 }
108 Mad = 1.4826 * Median(Data);
109
110 return Mad;
111}

◆ Randn()

double Randn ( double mu,
double sigma )

random generator based on calculations

Parameters
mu
sigma
Returns
double random number in the range of gaussian curve
122{
123 double U1, U2, W, mult;
124 static double X1, X2;
125 static int call = 0;
126
127 if (call == 1)
128 {
129 call = !call;
130 return (mu + sigma * (double)X2);
131 }
132
133 do
134 {
135 U1 = -1 + ((double)rand() / RAND_MAX) * 2;
136 U2 = -1 + ((double)rand() / RAND_MAX) * 2;
137 W = pow(U1, 2) + pow(U2, 2);
138 } while (W >= 1 || W == 0);
139
140 mult = sqrt((-2 * log(W)) / W);
141 X1 = U1 * mult;
142 X2 = U2 * mult;
143
144 call = !call;
145
146 return (mu + sigma * (double)X1);
147}
#define RAND_MAX
Maximum value that can be returned by the rand function.
Definition Transparency.h:35

◆ TestGaussianFromFile()

VOID TestGaussianFromFile ( )

A simple test for the data based on pre-defined numbers in a file.

Returns
VOID
225{
226 vector<double> MyVector;
227 UINT64 AverageOfData;
228 UINT64 StandardDeviationOfData;
229 UINT64 MedianOfData;
230
231 std::ifstream file("c:\\rev\\r.txt");
232 if (file.is_open())
233 {
234 std::string line;
235 while (std::getline(file, line))
236 {
237 MyVector.push_back(stod(line.c_str()));
238 }
239 file.close();
240
241 GuassianGenerateRandom(MyVector, &AverageOfData, &StandardDeviationOfData, &MedianOfData);
242 }
243}
VOID GuassianGenerateRandom(vector< double > Data, UINT64 *AverageOfData, UINT64 *StandardDeviationOfData, UINT64 *MedianOfData)
Calculate and generate random gaussian number.
Definition gaussian-rng.cpp:158