⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 newran.h

📁 随机数发生器C++写的
💻 H
📖 第 1 页 / 共 2 页
字号:
class NegativeBinomial : public AsymGen   // generate negative binomial rv{   Real N, P, Q, p, ln_q, c;public:   char* Name();   NegativeBinomial(Real NX, Real PX);    // constructor   Real Density(Real) const;              // Negative binomial density function   Real Next();   ExtReal Mean() const { return N * P; }   ExtReal Variance() const { return N * P * Q; }};//************************ sum of random numbers ************************class NegatedRandom : public Random{protected:   Random* rv;   NegatedRandom(Random& rvx) : rv(&rvx) {}   void tDelete() { rv->tDelete(); delete this; }public:   Real Next();   ExtReal Mean() const;   ExtReal Variance() const;   friend NegatedRandom& operator-(Random&);};class ScaledRandom : public Random{protected:   Random* rv; Real s;   ScaledRandom(Random& rvx, Real sx) : rv(&rvx), s(sx) {}   void tDelete() { rv->tDelete(); delete this; }public:   Real Next();   ExtReal Mean() const;   ExtReal Variance() const;   friend ScaledRandom& operator*(Real, Random&);   friend ScaledRandom& operator*(Random&, Real);   friend ScaledRandom& operator/(Random&, Real);};class ReciprocalRandom : public Random{protected:   Random* rv; Real s;   ReciprocalRandom(Random& rvx, Real sx) : rv(&rvx), s(sx) {}   void tDelete() { rv->tDelete(); delete this; }public:   Real Next();   ExtReal Mean() const { return Missing; }   ExtReal Variance() const { return Missing; }   friend ReciprocalRandom& operator/(Real, Random&);};class ShiftedRandom : public ScaledRandom{   ShiftedRandom(Random& rvx, Real sx) : ScaledRandom(rvx, sx) {}public:   Real Next();   ExtReal Mean() const;   ExtReal Variance() const;   friend ShiftedRandom& operator+(Real, Random&);   friend ShiftedRandom& operator+(Random&, Real);   friend ShiftedRandom& operator-(Random&, Real);};class ReverseShiftedRandom : public ScaledRandom{   ReverseShiftedRandom(Random& rvx, Real sx) : ScaledRandom(rvx, sx) {}public:   Real Next();   ExtReal Mean() const;   ExtReal Variance() const;   friend ReverseShiftedRandom& operator-(Real, Random&);};class RepeatedRandom : public Random{   Random* rv; int n;   void tDelete() { rv->tDelete(); delete this; }   RepeatedRandom(Random& rvx, int nx)  : rv(&rvx), n(nx) {}public:   Real Next();   ExtReal Mean() const;   ExtReal Variance() const;   friend class Random;};class MultipliedRandom : public Random{protected:   Random* rv1; Random* rv2;   void tDelete() { rv1->tDelete(); rv2->tDelete(); delete this; }   MultipliedRandom(Random& rv1x, Random& rv2x) : rv1(&rv1x), rv2(&rv2x) {}public:   Real Next();   ExtReal Mean() const;   ExtReal Variance() const;   friend MultipliedRandom& operator*(Random&, Random&);};class AddedRandom : public MultipliedRandom{   AddedRandom(Random& rv1x, Random& rv2x) : MultipliedRandom(rv1x, rv2x) {}public:   Real Next();   ExtReal Mean() const;   ExtReal Variance() const;   friend AddedRandom& operator+(Random&, Random&);};class SubtractedRandom : public MultipliedRandom{   SubtractedRandom(Random& rv1x, Random& rv2x)      : MultipliedRandom(rv1x, rv2x) {}public:   Real Next();   ExtReal Mean() const;   ExtReal Variance() const;   friend SubtractedRandom& operator-(Random&, Random&);};class DividedRandom : public MultipliedRandom{   DividedRandom(Random& rv1x, Random& rv2x)      : MultipliedRandom(rv1x, rv2x) {}public:   Real Next();   ExtReal Mean() const { return Missing; }   ExtReal Variance() const { return Missing; }   friend DividedRandom& operator/(Random&, Random&);};class SumRandom : public Random           // sum of random variables{   void operator=(const SumRandom&) {}    // private so can't access   Random* rv;public:   char* Name();                          // identification   SumRandom(NegatedRandom& rvx) : rv(&rvx) {}   SumRandom(AddedRandom& rvx) : rv(&rvx) {}   SumRandom(MultipliedRandom& rvx) : rv(&rvx) {}   SumRandom(SubtractedRandom& rvx) : rv(&rvx) {}   SumRandom(DividedRandom& rvx) : rv(&rvx) {}   SumRandom(ShiftedRandom& rvx) : rv(&rvx) {}   SumRandom(ReverseShiftedRandom& rvx) : rv(&rvx) {}   SumRandom(ScaledRandom& rvx) : rv(&rvx) {}   SumRandom(ReciprocalRandom& rvx) : rv(&rvx) {}   SumRandom(RepeatedRandom& rvx) : rv(&rvx) {}   Real Next() { return rv->Next(); }   ExtReal Mean() const { return rv->Mean(); }   ExtReal Variance() const { return rv->Variance(); }   ~SumRandom() { rv->tDelete(); }};//******************** mixtures of random numbers ************************class SelectedRandom : public Random{   friend class Random;   Random* rv; Real p;   SelectedRandom(Random& rvx, Real px) : rv(&rvx), p(px) {}   void tDelete() { rv->tDelete(); delete this; }public:   void load(int*, Real*, Random**);   Real Next();};class AddedSelectedRandom : public Random{   friend class Random;protected:   Random* rv1; Random* rv2;   AddedSelectedRandom(Random& rv1x, Random& rv2x)      : rv1(&rv1x), rv2(&rv2x) {}   void tDelete() { rv1->tDelete(); rv2->tDelete(); delete this; }public:   int nelems() const;   void load(int*, Real*, Random**);   Real Next();   friend AddedSelectedRandom& operator+(SelectedRandom&,      SelectedRandom&);   friend AddedSelectedRandom& operator+(AddedSelectedRandom&,      SelectedRandom&);   friend AddedSelectedRandom& operator+(SelectedRandom&,      AddedSelectedRandom&);   friend AddedSelectedRandom& operator+(AddedSelectedRandom&,      AddedSelectedRandom&);};class MixedRandom : public Random         // mixtures of random numbers{   void operator=(const MixedRandom&) {}  // private so can't access   int n;                                 // number of components   DiscreteGen* dg;                       // discrete mixing distribution   Random** rv;                           // array of pointers to rvs   ExtReal mean, var;   void Build(Real*);                     // used by constructorspublic:   char* Name();                          // identification   MixedRandom(int, Real*, Random**);   MixedRandom(AddedSelectedRandom&);   ~MixedRandom();   Real Next();   ExtReal Mean() const { return mean; }   ExtReal Variance() const { return var; }};//******************* Permutation generator *******************************class RandomPermutation                   // generate permutation of integers{   Random U;public:   void Next(int N, int M, int p[], int start = 0);                                          // select permutation of M numbers                                          // from start, ..., start+N-1                                          // results to p   void Next(int N, int p[], int start = 0) { Next(N, N, p, start); }};class RandomCombination : public RandomPermutation                                          // generate combination of integers                                          // sorted - ascending{   void SortAscending(int n, int gm[]);public:   void Next(int N, int M, int p[], int start = 0)      { RandomPermutation::Next(N, M, p, start); SortAscending(M, p); }   void Next(int N, int p[], int start = 0) { Next(N, N, p, start); }};//***************** Generators with variable parameters ********************class VariPoisson{   Uniform U;   Normal N;   Poisson P100;   Poisson P200;   int iNext_very_small(Real mu);   int iNext_small(Real mu);   int iNext_large(Real mu);public:   VariPoisson();   int iNext(Real mu);};class VariBinomial{   Uniform U;   Normal N;   int iNext_very_small(int n, Real p);   int iNext_small(int n, Real p);   int iNext_large(int n, Real p);public:   VariBinomial();   int iNext(int n, Real p);};class VariLogNormal{   Normal N;public:   VariLogNormal() {}   Real Next(Real mean, Real sd);};// friend functions declared again outside class definitionsNegatedRandom& operator-(Random&);ScaledRandom& operator*(Real, Random&);ScaledRandom& operator*(Random&, Real);ScaledRandom& operator/(Random&, Real);ReciprocalRandom& operator/(Real, Random&);ShiftedRandom& operator+(Real, Random&);ShiftedRandom& operator+(Random&, Real);ShiftedRandom& operator-(Random&, Real);ReverseShiftedRandom& operator-(Real, Random&);MultipliedRandom& operator*(Random&, Random&);AddedRandom& operator+(Random&, Random&);SubtractedRandom& operator-(Random&, Random&);DividedRandom& operator/(Random&, Random&);AddedSelectedRandom& operator+(SelectedRandom&, SelectedRandom&);AddedSelectedRandom& operator+(AddedSelectedRandom&, SelectedRandom&);AddedSelectedRandom& operator+(SelectedRandom&, AddedSelectedRandom&);AddedSelectedRandom& operator+(AddedSelectedRandom&, AddedSelectedRandom&);#ifdef use_namespace}#endif#endif// body file: newran.cpp

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -