SNAP Library 3.0, User Reference  2016-07-20 17:56:49
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TRnd Class Reference

#include <dt.h>

Public Member Functions

 TRnd (const int &_Seed=1, const int &Steps=0)
 
 TRnd (TSIn &SIn)
 
void Save (TSOut &SOut) const
 
void LoadXml (const PXmlTok &XmlTok, const TStr &Nm)
 
void SaveXml (TSOut &SOut, const TStr &Nm) const
 
TRndoperator= (const TRnd &Rnd)
 
bool operator== (const TRnd &) const
 
double GetUniDev ()
 
int GetUniDevInt (const int &Range=0)
 
int GetUniDevInt (const int &MnVal, const int &MxVal)
 
uint GetUniDevUInt (const uint &Range=0)
 
int64 GetUniDevInt64 (const int64 &Range=0)
 
uint64 GetUniDevUInt64 (const uint64 &Range=0)
 
double GetNrmDev ()
 
double GetNrmDev (const double &Mean, const double &SDev, const double &Mn, const double &Mx)
 
double GetExpDev ()
 
double GetExpDev (const double &Lambda)
 
double GetGammaDev (const int &Order)
 
double GetPoissonDev (const double &Mean)
 
double GetBinomialDev (const double &Prb, const int &Trials)
 
int GetGeoDev (const double &Prb)
 
double GetPowerDev (const double &AlphaSlope)
 
double GetRayleigh (const double &Sigma)
 
double GetWeibull (const double &K, const double &Lambda)
 
void PutSeed (const int &_Seed)
 
int GetSeed () const
 
void Randomize ()
 
void Move (const int &Steps)
 
bool Check ()
 
void SaveTxt (TOLx &Lx) const
 

Static Public Member Functions

static double GetUniDevStep (const int &Seed, const int &Steps)
 
static double GetNrmDevStep (const int &Seed, const int &Steps)
 
static double GetExpDevStep (const int &Seed, const int &Steps)
 
static TRnd LoadTxt (TILx &Lx)
 

Static Public Attributes

static const int RndSeed =0
 

Private Member Functions

int GetNextSeed ()
 

Private Attributes

int Seed
 

Static Private Attributes

static const int a =16807
 
static const int m =2147483647
 
static const int q =127773
 
static const int r =2836
 

Detailed Description

Definition at line 11 of file dt.h.

Constructor & Destructor Documentation

TRnd::TRnd ( const int &  _Seed = 1,
const int &  Steps = 0 
)
inline

Definition at line 20 of file dt.h.

20  {
21  PutSeed(_Seed); Move(Steps);}
void Move(const int &Steps)
Definition: dt.cpp:29
void PutSeed(const int &_Seed)
Definition: dt.cpp:18
TRnd::TRnd ( TSIn SIn)
inlineexplicit

Definition at line 22 of file dt.h.

22 {SIn.Load(Seed);}
void Load(bool &Bool)
Definition: fl.h:84
int Seed
Definition: dt.h:16

Member Function Documentation

bool TRnd::Check ( )

Definition at line 33 of file dt.cpp.

33  {
34  int PSeed=Seed; Seed=1;
35  for (int SeedN=0; SeedN<10000; SeedN++){GetNextSeed();}
36  bool Ok=Seed==1043618065; Seed=PSeed; return Ok;
37 }
int GetNextSeed()
Definition: dt.h:17
int Seed
Definition: dt.h:16
double TRnd::GetBinomialDev ( const double &  Prb,
const int &  Trials 
)

Definition at line 154 of file dt.cpp.

154  {
155  int j;
156  static int nold=(-1);
157  double am,em,g,angle,p,bnl,sq,t,y;
158  static double pold=(-1.0),pc,plog,pclog,en,oldg;
159 
160  p=(Prb <= 0.5 ? Prb : 1.0-Prb);
161  am=Trials*p;
162  if (Trials < 25) {
163  bnl=0.0;
164  for (j=1;j<=Trials;j++)
165  if (GetUniDev() < p) ++bnl;
166  } else if (am < 1.0) {
167  g=exp(-am);
168  t=1.0;
169  for (j=0;j<=Trials;j++) {
170  t *= GetUniDev();
171  if (t < g) break;
172  }
173  bnl=(j <= Trials ? j : Trials);
174  } else {
175  if (Trials != nold) {
176  en=Trials;
177  oldg=TSpecFunc::LnGamma(en+1.0);
178  nold=Trials;
179  } if (p != pold) {
180  pc=1.0-p;
181  plog=log(p);
182  pclog=log(pc);
183  pold=p;
184  }
185  sq=sqrt(2.0*am*pc);
186  do {
187  do {
188  angle=TMath::Pi*GetUniDev();
189  y=tan(angle);
190  em=sq*y+am;
191  } while (em < 0.0 || em >= (en+1.0));
192  em=floor(em);
193  t=1.2*sq*(1.0+y*y)*exp(oldg-(em+1.0)
194  -TSpecFunc::LnGamma(en-em+1.0)+em*plog+(en-em)*pclog);
195  } while (GetUniDev() > t);
196  bnl=em;
197  }
198  if (p != Prb) bnl=Trials-bnl;
199  return bnl;
200 }
static double LnGamma(const double &xx)
Definition: xmath.cpp:80
static double Pi
Definition: xmath.h:8
double GetUniDev()
Definition: dt.h:30
double TRnd::GetExpDev ( )

Definition at line 83 of file dt.cpp.

83  {
84  double UniDev;
85  do {
86  UniDev=GetUniDev();
87  } while (UniDev==0.0);
88  return -log(UniDev);
89 }
double GetUniDev()
Definition: dt.h:30
double TRnd::GetExpDev ( const double &  Lambda)

Definition at line 91 of file dt.cpp.

91  {
92  return GetExpDev()/Lambda;
93 }
double GetExpDev()
Definition: dt.cpp:83
static double TRnd::GetExpDevStep ( const int &  Seed,
const int &  Steps 
)
inlinestatic

Definition at line 68 of file dt.h.

68  {
69  TRnd Rnd(Seed); Rnd.Move(Steps); return Rnd.GetExpDev();}
Definition: dt.h:11
int Seed
Definition: dt.h:16
double TRnd::GetGammaDev ( const int &  Order)

Definition at line 95 of file dt.cpp.

95  {
96  int j;
97  double am,e,s,v1,v2,x,y;
98  if (Order<1){Fail;}
99  if (Order<6) {
100  x=1.0;
101  for (j=1;j<=Order;j++) x *=GetUniDev();
102  x = -log(x);
103  } else {
104  do {
105  do {
106  do {
107  v1=2.0*GetUniDev()-1.0;
108  v2=2.0*GetUniDev()-1.0;
109  } while (v1*v1+v2*v2 > 1.0);
110  y=v2/v1;
111  am=Order-1;
112  s=sqrt(2.0*am+1.0);
113  x=s*y+am;
114  } while (x <= 0.0);
115  e=(1.0+y*y)*exp(am*log(x/am)-s*y);
116  } while (GetUniDev()>e);
117  }
118  return x;
119 }
#define Fail
Definition: bd.h:238
double GetUniDev()
Definition: dt.h:30
int TRnd::GetGeoDev ( const double &  Prb)
inline

Definition at line 45 of file dt.h.

45  {
46  return 1+(int)floor(log(1.0-GetUniDev())/log(1.0-Prb));}
double GetUniDev()
Definition: dt.h:30
int TRnd::GetNextSeed ( )
inlineprivate

Definition at line 17 of file dt.h.

17  {
18  if ((Seed=a*(Seed%q)-r*(Seed/q))>0){return Seed;} else {return Seed+=m;}}
static const int r
Definition: dt.h:15
static const int q
Definition: dt.h:15
static const int a
Definition: dt.h:15
int Seed
Definition: dt.h:16
static const int m
Definition: dt.h:15
double TRnd::GetNrmDev ( )

Definition at line 63 of file dt.cpp.

63  {
64  double v1, v2, rsq;
65  do {
66  v1=2.0*GetUniDev()-1.0; // pick two uniform numbers in the square
67  v2=2.0*GetUniDev()-1.0; // extending from -1 to +1 in each direction
68  rsq=v1*v1+v2*v2; // see if they are in the unit cicrcle
69  } while ((rsq>=1.0)||(rsq==0.0)); // and if they are not, try again
70  double fac=sqrt(-2.0*log(rsq)/rsq); // Box-Muller transformation
71  return v1*fac;
72 // return v2*fac; // second deviate
73 }
double GetUniDev()
Definition: dt.h:30
double TRnd::GetNrmDev ( const double &  Mean,
const double &  SDev,
const double &  Mn,
const double &  Mx 
)

Definition at line 75 of file dt.cpp.

76  {
77  double Val=Mean+GetNrmDev()*SDev;
78  if (Val<Mn){Val=Mn;}
79  if (Val>Mx){Val=Mx;}
80  return Val;
81 }
double GetNrmDev()
Definition: dt.cpp:63
static double TRnd::GetNrmDevStep ( const int &  Seed,
const int &  Steps 
)
inlinestatic

Definition at line 66 of file dt.h.

66  {
67  TRnd Rnd(Seed); Rnd.Move(Steps); return Rnd.GetNrmDev();}
Definition: dt.h:11
int Seed
Definition: dt.h:16
double TRnd::GetPoissonDev ( const double &  Mean)

Definition at line 121 of file dt.cpp.

121  {
122  static double sq,alxm,g,oldm=(-1.0);
123  double em,t,y;
124  if (Mean < 12.0) {
125  if (Mean != oldm) {
126  oldm=Mean;
127  g=exp(-Mean);
128  }
129  em = -1;
130  t=1.0;
131  do {
132  ++em;
133  t *= GetUniDev();
134  } while (t>g);
135  } else {
136  if (Mean != oldm) {
137  oldm=Mean;
138  sq=sqrt(2.0*Mean);
139  alxm=log(Mean);
140  g=Mean*alxm-TSpecFunc::LnGamma(Mean+1.0);
141  }
142  do {
143  do {
144  y=tan(TMath::Pi*GetUniDev());
145  em=sq*y+Mean;
146  } while (em < 0.0);
147  em=floor(em);
148  t=0.9*(1.0+y*y)*exp(em*alxm-TSpecFunc::LnGamma(em+1.0)-g);
149  } while (GetUniDev()>t);
150  }
151  return em;
152 }
static double LnGamma(const double &xx)
Definition: xmath.cpp:80
static double Pi
Definition: xmath.h:8
double GetUniDev()
Definition: dt.h:30
double TRnd::GetPowerDev ( const double &  AlphaSlope)
inline

Definition at line 47 of file dt.h.

47  { // power-law degree distribution (AlphaSlope>0)
48  IAssert(AlphaSlope>1.0);
49  return pow(1.0-GetUniDev(), -1.0/(AlphaSlope-1.0));}
#define IAssert(Cond)
Definition: bd.h:262
double GetUniDev()
Definition: dt.h:30
double TRnd::GetRayleigh ( const double &  Sigma)
inline

Definition at line 50 of file dt.h.

50  { // 1/sqrt(alpha) = sigma
51  IAssert(Sigma>0.0);
52  return Sigma*sqrt(-2*log(1-GetUniDev()));}
#define IAssert(Cond)
Definition: bd.h:262
double GetUniDev()
Definition: dt.h:30
int TRnd::GetSeed ( ) const
inline

Definition at line 59 of file dt.h.

59 {return Seed;}
int Seed
Definition: dt.h:16
double TRnd::GetUniDev ( )
inline

Definition at line 30 of file dt.h.

30 {return GetNextSeed()/double(m);}
int GetNextSeed()
Definition: dt.h:17
static const int m
Definition: dt.h:15
int TRnd::GetUniDevInt ( const int &  Range = 0)

Definition at line 39 of file dt.cpp.

39  {
40  int Seed=GetNextSeed();
41  if (Range==0){return Seed;}
42  else {return Seed%Range;}
43 }
int GetNextSeed()
Definition: dt.h:17
int Seed
Definition: dt.h:16
int TRnd::GetUniDevInt ( const int &  MnVal,
const int &  MxVal 
)
inline

Definition at line 32 of file dt.h.

32  {
33  IAssert(MnVal<=MxVal); return MnVal+GetUniDevInt(MxVal-MnVal+1);}
#define IAssert(Cond)
Definition: bd.h:262
int GetUniDevInt(const int &Range=0)
Definition: dt.cpp:39
int64 TRnd::GetUniDevInt64 ( const int64 Range = 0)

Definition at line 51 of file dt.cpp.

51  {
52  const int64 RndVal = int64((uint64(GetUniDevInt())<<32) | uint64(GetUniDevInt()));
53  if (Range==0){return RndVal;}
54  else {return RndVal%Range;}
55 }
unsigned long long uint64
Definition: bd.h:38
long long int64
Definition: bd.h:27
int GetUniDevInt(const int &Range=0)
Definition: dt.cpp:39
static double TRnd::GetUniDevStep ( const int &  Seed,
const int &  Steps 
)
inlinestatic

Definition at line 64 of file dt.h.

64  {
65  TRnd Rnd(Seed); Rnd.Move(Steps); return Rnd.GetUniDev();}
Definition: dt.h:11
int Seed
Definition: dt.h:16
uint TRnd::GetUniDevUInt ( const uint Range = 0)

Definition at line 45 of file dt.cpp.

45  {
46  uint Seed=uint(GetNextSeed()%0x10000)*0x10000+uint(GetNextSeed()%0x10000);
47  if (Range==0){return Seed;}
48  else {return Seed%Range;}
49 }
int GetNextSeed()
Definition: dt.h:17
unsigned int uint
Definition: bd.h:11
int Seed
Definition: dt.h:16
uint64 TRnd::GetUniDevUInt64 ( const uint64 Range = 0)

Definition at line 57 of file dt.cpp.

57  {
58  const uint64 RndVal = uint64((uint64(GetUniDevInt())<<32) | uint64(GetUniDevInt()));
59  if (Range==0){return RndVal;}
60  else {return RndVal%Range;}
61 }
unsigned long long uint64
Definition: bd.h:38
int GetUniDevInt(const int &Range=0)
Definition: dt.cpp:39
double TRnd::GetWeibull ( const double &  K,
const double &  Lambda 
)
inline

Definition at line 53 of file dt.h.

53  { // 1/alpha = lambda
54  IAssert(Lambda>0.0 && K>0.0);
55  return Lambda*pow(-log(1-GetUniDev()), 1.0/K);}
#define IAssert(Cond)
Definition: bd.h:262
double GetUniDev()
Definition: dt.h:30
TRnd TRnd::LoadTxt ( TILx Lx)
static

Definition at line 215 of file dt.cpp.

215  {
216  return TRnd(Lx.GetInt());
217 }
int GetInt()
Definition: lx.h:181
TRnd(const int &_Seed=1, const int &Steps=0)
Definition: dt.h:20
void TRnd::LoadXml ( const PXmlTok XmlTok,
const TStr Nm 
)

Definition at line 9 of file dt.cpp.

9  {
10  XLoadHd(Nm);
11  Seed=TXmlObjSer::GetIntArg(XmlTok, "Seed");
12 }
#define XLoadHd(Nm)
Definition: bd.h:312
static int GetIntArg(const PXmlTok &XmlTok, const TStr &Nm)
Definition: xml.cpp:74
int Seed
Definition: dt.h:16
void TRnd::Move ( const int &  Steps)

Definition at line 29 of file dt.cpp.

29  {
30  for (int StepN=0; StepN<Steps; StepN++){GetNextSeed();}
31 }
int GetNextSeed()
Definition: dt.h:17
TRnd& TRnd::operator= ( const TRnd Rnd)
inline

Definition at line 27 of file dt.h.

27 {Seed=Rnd.Seed; return *this;}
int Seed
Definition: dt.h:16
bool TRnd::operator== ( const TRnd ) const
inline

Definition at line 28 of file dt.h.

28 {Fail; return false;}
#define Fail
Definition: bd.h:238
void TRnd::PutSeed ( const int &  _Seed)

Definition at line 18 of file dt.cpp.

18  {
19  Assert(_Seed>=0);
20  if (_Seed==0){
21  //Seed=int(time(NULL));
22  Seed=abs(int(TSysTm::GetPerfTimerTicks()));
23  } else {
24  Seed=_Seed;
25  //Seed=abs(_Seed*100000)+1;
26  }
27 }
#define Assert(Cond)
Definition: bd.h:251
int Seed
Definition: dt.h:16
void TRnd::Randomize ( )
inline

Definition at line 60 of file dt.h.

60 {PutSeed(RndSeed);}
static const int RndSeed
Definition: dt.h:13
void PutSeed(const int &_Seed)
Definition: dt.cpp:18
void TRnd::Save ( TSOut SOut) const
inline

Definition at line 23 of file dt.h.

23 {SOut.Save(Seed);}
void Save(const bool &Bool)
Definition: fl.h:173
int Seed
Definition: dt.h:16
void TRnd::SaveTxt ( TOLx Lx) const

Definition at line 219 of file dt.cpp.

219  {
220  Lx.PutInt(Seed);
221 }
void PutInt(const TInt &Int)
Definition: lx.h:277
int Seed
Definition: dt.h:16
void TRnd::SaveXml ( TSOut SOut,
const TStr Nm 
) const

Definition at line 14 of file dt.cpp.

14  {
15  XSaveBETagArg(Nm, "Seed", TInt::GetStr(Seed));
16 }
TStr GetStr() const
Definition: dt.h:1107
#define XSaveBETagArg(Nm, ArgNm, ArgVal)
Definition: bd.h:327
int Seed
Definition: dt.h:16

Member Data Documentation

const int TRnd::a =16807
staticprivate

Definition at line 15 of file dt.h.

const int TRnd::m =2147483647
staticprivate

Definition at line 15 of file dt.h.

const int TRnd::q =127773
staticprivate

Definition at line 15 of file dt.h.

const int TRnd::r =2836
staticprivate

Definition at line 15 of file dt.h.

const int TRnd::RndSeed =0
static

Definition at line 13 of file dt.h.

int TRnd::Seed
private

Definition at line 16 of file dt.h.


The documentation for this class was generated from the following files: