1////////////////////////////////////////////////////////////////////////////////
2// //
3// This software is supplied under the terms of a license agreement or //
4// nondisclosure agreement with Mitov Software and may not be copied //
5// or disclosed except in accordance with the terms of that agreement. //
6// Copyright(c) 2002-2016 Mitov Software. All Rights Reserved. //
7// //
8////////////////////////////////////////////////////////////////////////////////
9
10#ifndef _MITOV_GAUSS_GENERATOR_h
11#define _MITOV_GAUSS_GENERATOR_h
12
13#include <Mitov.h>
14#include "Mitov_BasicGenerator.h"
15
16namespace Mitov
17{
18 template<typename T> class CommonGaussGenerator : public Mitov::BasicGenerator<T>
19 {
20 typedef Mitov::BasicGenerator<T> inherited;
21
22 public:
23 T Mean;
24 T StandardDeviation;
25 long Seed = 0;
26
27 protected:
28 uint8_t FIndex = 0;
29 T FRandoms[ 2 ];
30
31 protected:
32 virtual void SystemStart()
33 {
34 randomSeed( Seed );
35
36 inherited::SystemStart();
37 }
38
39 virtual void SystemLoopBegin( unsigned long currentMicros )
40 {
41 if( ! inherited::ClockInputPin.IsConnected() )
42 Generate();
43
44 inherited::SystemLoopBegin( currentMicros );
45 }
46
47 protected:
48 void Generate()
49 {
50 if( inherited::Enabled )
51 {
52 if( ! FIndex )
53 {
54 float W;
55 float X1;
56 float X2;
57 do
58 {
59 X1 = random( -1147483648, 1147483647 ) / 1147483647.0;
60 X2 = random( -1147483648, 1147483647 ) / 1147483647.0;
61 W = X1 * X1 + X2 * X2;
62 }
63 while( W >= 1.0 );
64
65 W = StandardDeviation * sqrt( (-2.0 * log( W ) ) / W );
66 Serial.println( W );
67 Serial.println( Mean );
68 Serial.println( X1 );
69 Serial.print( "RESULT1: " ); Serial.println( Mean + X1 * W );
70 Serial.println( X2 );
71 Serial.print( "RESULT2: " ); Serial.println( Mean + X2 * W );
72 FRandoms[ 0 ] = Mean + X1 * W;
73 FRandoms[ 1 ] = Mean + X2 * W;
74 }
75
76 inherited::FValue = FRandoms[ FIndex ];
77 FIndex = FIndex ^ 1;
78 }
79
80 inherited::SendOutput();
81 }
82
83 virtual void DoClockReceive( void *_Data )
84 {
85 Generate();
86 }
87
88 public:
89 CommonGaussGenerator( T AMean, T AStandardDeviation ) :
90 Mean( AMean ),
91 StandardDeviation( AStandardDeviation )
92 {
93 }
94
95 };
96//---------------------------------------------------------------------------
97}
98
99#endif