OpendTect-6_4  6.4
statrand.h
Go to the documentation of this file.
1 #ifndef statrand_h
2 #define statrand_h
3 /*+
4 ________________________________________________________________________
5 
6  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
7  Author: Bert Bril
8  Date: Sep 2006
9  RCS: $Id$
10 ________________________________________________________________________
11 
12 -*/
13 
14 #include "algomod.h"
15 #include "gendefs.h"
16 #include "odset.h"
17 
18 namespace Stats
19 {
20 
24 {
25 public:
26 
27  virtual ~RandomGenerator() {}
28  virtual void init(int seed);
29  // seed != 0 gives repeatable numbers
30 
31  virtual double get() const = 0; // returns in [0,1]
32 
33 };
34 
35 
39 {
40 public:
41 
42  RandGen();
43 
44  virtual double get() const;
46  int getInt() const;
48  int getIndex(int sz) const;
50  int getIndexFast(int sz,int seed) const;
52  od_int64 getIndex(od_int64 sz) const;
54  od_int64 getIndexFast(od_int64 sz,od_int64 seed) const;
56 
57  template <class T,class SzTp>
58  void subselect(T*,SzTp sz,SzTp targetsz) const;
62  template <class T>
63  void subselect(T&,od_int64 targetsz) const;
66 
67 private:
68 
69  int seed_;
70 
71 };
72 
73 
74 mGlobal(Algo) RandGen randGen();
75 
76 
78 {
79 public:
80 
81  NormalRandGen();
82 
83  virtual double get() const;
84  float get(float expect,float stdev) const;
85  double get(double expect,double stdev) const;
86 
87 protected:
88 
89  mutable bool useothdrawn_;
90  mutable double othdrawn_;
91 
92 };
93 
94 
95 template <class T,class SzTp>
96 inline void Stats::RandGen::subselect( T* arr, SzTp sz, SzTp targetsz ) const
97 {
98  for ( SzTp idx=sz-1; idx>=targetsz; idx-- )
99  {
100  const SzTp notselidx = getIndex( idx );
101  if ( notselidx != idx )
102  Swap( arr[notselidx], arr[idx] );
103  }
104 }
105 
106 
107 template <class ODSET>
108 inline void Stats::RandGen::subselect( ODSET& ods, od_int64 targetsz ) const
109 {
110  typedef typename ODSET::size_type size_type;
111  const size_type sz = ods.size();
112  if ( sz <= targetsz ) return;
113 
114  for ( size_type idx=sz-1; idx>=targetsz; idx-- )
115  {
116  const size_type notselidx = getIndex( idx );
117  if ( notselidx != idx )
118  ods.swap( notselidx, idx );
119  }
120 
121  removeRange( ods, (size_type)targetsz, sz-1 );
122 }
123 
124 }; // namespace Stats
125 
126 
127 #endif
#define mExpClass(module)
Definition: commondefs.h:160
Definition: statrand.h:77
virtual ~RandomGenerator()
Definition: statrand.h:27
Uniform Random Generator.
Definition: statrand.h:38
#define mGlobal(module)
Definition: commondefs.h:163
#define od_int64
Definition: plftypes.h:36
bool init()
double othdrawn_
Definition: statrand.h:90
void subselect(T *, SzTp sz, SzTp targetsz) const
Definition: statrand.h:96
RandGen randGen()
int seed_
Definition: statrand.h:69
Random Generator.
Definition: statrand.h:23
void Swap(T &a, T &b)
Definition: commondefs.h:36
void removeRange(ODSET &inst, size_type start, size_type stop)
Removes a range from the set.
Definition: odset.h:57
Statistics.
Definition: array2dinterpol.h:28
bool useothdrawn_
Definition: statrand.h:89

Generated at for the OpendTect seismic interpretation project. Copyright (C): dGB Beheer B. V. 2019