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

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