libraries / Mitov / Mitov_RandomGenerator.hon commit Added link to project report (97a3ba0)
   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_RANDOM_GENERATOR_h
  11#define _MITOV_RANDOM_GENERATOR_h
  12
  13#include <Mitov.h>
  14#include "Mitov_BasicGenerator.h"
  15
  16namespace Mitov
  17{
  18#define Min Min
  19#define Max Max
  20        template<typename T> class CommonRandomGenerator : public Mitov::BasicGenerator<T>
  21        {
  22                typedef Mitov::BasicGenerator<T> inherited;
  23
  24        public:
  25                T       Min;
  26                T       Max;
  27                long    Seed = 0;
  28
  29        protected:
  30                virtual void SystemStart()
  31                {
  32                        randomSeed( Seed );
  33
  34                        inherited::SystemStart();
  35                }
  36
  37                virtual void SystemLoopBegin( unsigned long currentMicros )
  38                {
  39                        if( ! inherited::ClockInputPin.IsConnected() )
  40                                Generate();
  41
  42                        inherited::SystemLoopBegin( currentMicros );
  43                }
  44
  45        protected:
  46                virtual void GenerateValue() = 0;
  47
  48                void Generate()
  49                {
  50            if( inherited::Enabled )
  51            {
  52                                if( Min == Max )
  53                                        inherited::FValue = Min;
  54
  55                                else
  56                                        GenerateValue();
  57                        }
  58
  59                        inherited::SendOutput();
  60                }
  61
  62                virtual void DoClockReceive( void *_Data )
  63                {
  64                        Generate();
  65                }
  66
  67        public:
  68                CommonRandomGenerator( T AMin, T AMax ) :
  69                        Min( AMin ),
  70                        Max( AMax )
  71                {
  72                }
  73
  74        };
  75//---------------------------------------------------------------------------
  76        class RandomAnalogGenerator : public Mitov::CommonRandomGenerator<float>
  77        {
  78                typedef Mitov::CommonRandomGenerator<float> inherited;
  79
  80        protected:
  81                virtual void GenerateValue()
  82                {
  83                        float AMin = MitovMin( Min, Max );
  84                        float AMax = MitovMax( Min, Max );
  85//                      double ARandom = random( -2147483648, 2147483647 );
  86                        double ARandom = random( -1147483648, 1147483647 );
  87//                      FValue = ARandom;
  88                        ARandom += 1147483648;
  89                        FValue = AMin + ( ARandom / ( (double)1147483647 + (double)1147483648 )) * (AMax - AMin);
  90                }
  91
  92        public:
  93                RandomAnalogGenerator() :
  94                        inherited( 0, 1 )
  95                {
  96                }
  97
  98        };
  99//---------------------------------------------------------------------------
 100        class RandomIntegerGenerator : public Mitov::CommonRandomGenerator<long>
 101        {
 102                typedef Mitov::CommonRandomGenerator<long> inherited;
 103
 104        protected:
 105                virtual void GenerateValue()
 106                {
 107                        long AMin = MitovMin( Min, Max );
 108                        long AMax = MitovMax( Min, Max );
 109                        FValue = random( AMin, AMax + 1 );
 110                }
 111
 112        public:
 113                RandomIntegerGenerator() :
 114                        inherited( -1000, 1000 )
 115                {
 116                }
 117
 118        };
 119//---------------------------------------------------------------------------
 120        class RandomUnsignedGenerator : public Mitov::CommonRandomGenerator<unsigned long>
 121        {
 122                typedef Mitov::CommonRandomGenerator<unsigned long> inherited;
 123
 124        protected:
 125                virtual void GenerateValue()
 126                {
 127                        unsigned long AMin = MitovMin( Min, Max );
 128                        unsigned long AMax = MitovMax( Min, Max );
 129                        FValue = random( AMin, AMax + 1 );
 130                }
 131
 132        public:
 133                RandomUnsignedGenerator() :
 134                        inherited( 0, 1000 )
 135                {
 136                }
 137
 138        };
 139//---------------------------------------------------------------------------
 140        class RandomDateTimeGenerator : public Mitov::CommonRandomGenerator<Mitov::TDateTime>
 141        {
 142                typedef Mitov::CommonRandomGenerator<Mitov::TDateTime> inherited;
 143
 144        protected:
 145                virtual void GenerateValue()
 146                {
 147                        uint32_t ARandomDate = random( Min.Date, Max.Date );
 148                        uint32_t ARandomTime;
 149                        if( ARandomDate == Min.Date )
 150                                ARandomTime = random( Min.Time, MSecsPerDay );
 151
 152                        else if( ARandomDate == Max.Date )
 153                                ARandomTime = random( 0, Max.Time );
 154
 155                        else
 156                                ARandomTime = random( 0, MSecsPerDay );
 157
 158                        FValue.Date = ARandomDate;
 159                        FValue.Time = ARandomTime;
 160/*
 161                        float AMin = MitovMin( Min.Value, Max.Value );
 162                        float AMax = MitovMax( Min.Value, Max.Value );
 163//                      double ARandom = random( -2147483648, 2147483647 );
 164                        double ARandom = random( -1147483648, 1147483647 );
 165//                      FValue = ARandom;
 166                        ARandom += 1147483648;
 167                        FValue.Value = AMin + ( ARandom / ( (double)1147483647 + (double)1147483648 )) * (AMax - AMin);
 168*/
 169                }
 170
 171        public:
 172                RandomDateTimeGenerator() :
 173                        inherited( Mitov::TDateTime( 693594, 0 ), Mitov::TDateTime( 693694, 0 ) )
 174                {
 175//                      Min(  )
 176                }
 177
 178        };
 179//---------------------------------------------------------------------------
 180#undef Min
 181#undef Max
 182}
 183
 184#endif