SNAP Library 6.0, Developer Reference  2020-12-09 16:24:20
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
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.

References Move(), and PutSeed().

Referenced by LoadTxt().

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

Here is the call graph for this function:

Here is the caller graph for this function:

TRnd::TRnd ( TSIn SIn)
inlineexplicit

Definition at line 22 of file dt.h.

References TSIn::Load().

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

Here is the call graph for this function:

Member Function Documentation

bool TRnd::Check ( )

Definition at line 33 of file dt.cpp.

References GetNextSeed(), and Seed.

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

Here is the call graph for this function:

double TRnd::GetBinomialDev ( const double &  Prb,
const int &  Trials 
)

Definition at line 154 of file dt.cpp.

References GetUniDev(), TSpecFunc::LnGamma(), and TMath::Pi.

Referenced by TAGM::RndConnectInsideCommunity().

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

Here is the call graph for this function:

Here is the caller graph for this function:

double TRnd::GetExpDev ( )

Definition at line 83 of file dt.cpp.

References GetUniDev().

Referenced by TNetInfBs::GenCascade(), TNIBs::GenCascade(), GetExpDev(), and GetExpDevStep().

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

Here is the call graph for this function:

Here is the caller graph for this function:

double TRnd::GetExpDev ( const double &  Lambda)

Definition at line 91 of file dt.cpp.

References GetExpDev().

91  {
92  return GetExpDev()/Lambda;
93 }
double GetExpDev()
Definition: dt.cpp:83

Here is the call graph for this function:

static double TRnd::GetExpDevStep ( const int &  Seed,
const int &  Steps 
)
inlinestatic

Definition at line 68 of file dt.h.

References GetExpDev(), and Move().

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

Here is the call graph for this function:

double TRnd::GetGammaDev ( const int &  Order)

Definition at line 95 of file dt.cpp.

References Fail, and GetUniDev().

Referenced by TMAGNodeBeta::AttrGen().

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

Here is the call graph for this function:

Here is the caller graph for this function:

int TRnd::GetGeoDev ( const double &  Prb)
inline

Definition at line 45 of file dt.h.

References GetUniDev().

Referenced by TForestFire::BurnGeoFire(), and TUndirFFire::BurnGeoFire().

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

Here is the call graph for this function:

Here is the caller graph for this function:

int TRnd::GetNextSeed ( )
inlineprivate

Definition at line 17 of file dt.h.

References m, and Seed.

Referenced by Check(), GetUniDev(), GetUniDevInt(), GetUniDevUInt(), and Move().

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

Here is the caller graph for this function:

double TRnd::GetNrmDev ( )

Definition at line 63 of file dt.cpp.

References GetUniDev().

Referenced by TMAGAffMtx::AddRndNoise(), TKronMtx::AddRndNoise(), TLAMisc::FillRnd(), GetNrmDev(), GetNrmDevStep(), and TSnap::TSnapDetail::GetSphereDev().

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

Here is the call graph for this function:

Here is the caller graph for this function:

double TRnd::GetNrmDev ( const double &  Mean,
const double &  SDev,
const double &  Mn,
const double &  Mx 
)

Definition at line 75 of file dt.cpp.

References GetNrmDev().

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

Here is the call graph for this function:

static double TRnd::GetNrmDevStep ( const int &  Seed,
const int &  Steps 
)
inlinestatic

Definition at line 66 of file dt.h.

References GetNrmDev(), and Move().

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

Here is the call graph for this function:

double TRnd::GetPoissonDev ( const double &  Mean)

Definition at line 121 of file dt.cpp.

References GetUniDev(), TSpecFunc::LnGamma(), and TMath::Pi.

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

Here is the call graph for this function:

double TRnd::GetPowerDev ( const double &  AlphaSlope)
inline

Definition at line 47 of file dt.h.

References GetUniDev(), and IAssert.

Referenced by TNetInfBs::GenCascade(), TNIBs::GenCascade(), TAGMUtil::GenPLSeq(), and TSnap::GenRndPowerLaw().

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

Here is the call graph for this function:

Here is the caller graph for this function:

double TRnd::GetRayleigh ( const double &  Sigma)
inline

Definition at line 50 of file dt.h.

References GetUniDev(), and IAssert.

Referenced by TNetInfBs::GenCascade(), and TNIBs::GenCascade().

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

Here is the call graph for this function:

Here is the caller graph for this function:

int TRnd::GetSeed ( ) const
inline

Definition at line 59 of file dt.h.

References Seed.

59 {return Seed;}
int Seed
Definition: dt.h:16
int TRnd::GetUniDevInt ( const int &  Range = 0)

Definition at line 39 of file dt.cpp.

References GetNextSeed(), and Seed.

Referenced by TFfGGen::AddNodes(), TUndirFFire::AddNodes(), TNIBs::BSG(), TAGMUtil::ConnectCmtyVV(), TMAGFitBern::DoEStepApxOneIter(), TMAGFitBern::DoEStepOneIter(), TAGMFast::FindComsByCV(), TKronNoise::FlipEdgeNoise(), TSnap::GenConfModel(), TSnap::GenCopyModel(), TSnap::GenGeoPrefAttach(), TCesna::GenHoldOutAttr(), TCesnaUtil::GenHoldOutPairs(), TAGMFastUtil::GenHoldOutPairs(), TSnap::GenPrefAttach(), TTree< TVal >::GenRandomTree(), TSnap::GenRndBipart(), TSnap::GenRndGnm(), TSnap::GenSmallWorld(), TTimeNENet::GetGnmRndNet(), TSnap::GetMxDegNId(), TSnap::GetMxInDegNId(), TSnap::GetMxOutDegNId(), TTimeNENet::GetPrefAttach(), TBool::GetRnd(), TInt::GetRnd(), TSnap::TSnapDetail::GetRndEdgeNonAdjNode(), THash< TKey, TDat, THashFunc >::GetRndKeyId(), THashSet< TInt >::GetRndKeyId(), TCnCom::GetRndNId(), TBPGraph::GetRndNId(), TBreathFS< PGraph >::GetRndPath(), GetUniDevInt(), GetUniDevInt64(), GetUniDevUInt64(), TKroneckerLL::MetroGibbsSampleNext(), TAGMFast::NeighborComInit(), TCoda::NeighborComInit(), TCesna::NeighborComInit(), TAGMFast::RandomInit(), TCoda::RandomInit(), TAGMFit::RandomInit(), TCesna::RandomInit(), TAGMUtil::RewireCmtyNID(), TAGM::RndConnectInsideCommunity(), TKroneckerLL::SampleNextPerm(), TSnap::SamplePersonalizedPageRank(), TAGMFit::SampleTransition(), TNIBs::SG(), TVec< TVal, TSizeTy >::Shuffle(), TVecPool< TVal, TSizeTy >::ShuffleAll(), TGLib_OLD::TVecPool< TVal >::ShuffleAll(), TVVec< TVal, TSizeTy >::ShuffleX(), TVVec< TVal, TSizeTy >::ShuffleY(), SimulateWalk(), TUniChDb::TestFindNextWordOrSentenceBoundary(), TCluster::Train(), and TrainModel().

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

Here is the call graph for this function:

Here is the caller graph for this function:

int TRnd::GetUniDevInt ( const int &  MnVal,
const int &  MxVal 
)
inline

Definition at line 32 of file dt.h.

References GetUniDevInt(), and IAssert.

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

Here is the call graph for this function:

int64 TRnd::GetUniDevInt64 ( const int64 Range = 0)

Definition at line 51 of file dt.cpp.

References GetUniDevInt().

Referenced by TVec< TVal, TSizeTy >::Shuffle().

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

Here is the call graph for this function:

Here is the caller graph for this function:

static double TRnd::GetUniDevStep ( const int &  Seed,
const int &  Steps 
)
inlinestatic

Definition at line 64 of file dt.h.

References GetUniDev(), and Move().

64  {
65  TRnd Rnd(Seed); Rnd.Move(Steps); return Rnd.GetUniDev();}
Definition: dt.h:11
int Seed
Definition: dt.h:16

Here is the call graph for this function:

uint TRnd::GetUniDevUInt ( const uint Range = 0)

Definition at line 45 of file dt.cpp.

References GetNextSeed(), and Seed.

Referenced by TUInt::GetRnd(), and TUniCodec::GetRndUint().

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

Here is the call graph for this function:

Here is the caller graph for this function:

uint64 TRnd::GetUniDevUInt64 ( const uint64 Range = 0)

Definition at line 57 of file dt.cpp.

References GetUniDevInt().

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

Here is the call graph for this function:

double TRnd::GetWeibull ( const double &  K,
const double &  Lambda 
)
inline

Definition at line 53 of file dt.h.

References GetUniDev(), and IAssert.

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

Here is the call graph for this function:

TRnd TRnd::LoadTxt ( TILx Lx)
static

Definition at line 215 of file dt.cpp.

References TILx::GetInt(), and TRnd().

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

Here is the call graph for this function:

void TRnd::LoadXml ( const PXmlTok XmlTok,
const TStr Nm 
)

Definition at line 9 of file dt.cpp.

References TXmlObjSer::GetIntArg(), Seed, and XLoadHd.

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

Here is the call graph for this function:

void TRnd::Move ( const int &  Steps)

Definition at line 29 of file dt.cpp.

References GetNextSeed().

Referenced by GetExpDevStep(), GetNrmDevStep(), GetUniDevStep(), and TRnd().

29  {
30  for (int StepN=0; StepN<Steps; StepN++){GetNextSeed();}
31 }
int GetNextSeed()
Definition: dt.h:17

Here is the call graph for this function:

Here is the caller graph for this function:

TRnd& TRnd::operator= ( const TRnd Rnd)
inline

Definition at line 27 of file dt.h.

References Seed.

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.

References Fail.

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

Definition at line 18 of file dt.cpp.

References Assert, and Seed.

Referenced by TAGMFit::Load(), TAGMFast::Load(), TCoda::Load(), TKronMtx::PutRndSeed(), TMAGFitBern::RandomInit(), Randomize(), and TRnd().

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

Here is the caller graph for this function:

void TRnd::Randomize ( )
inline

Definition at line 60 of file dt.h.

References PutSeed().

Referenced by TNIBs::GenCascade(), and TSnap::Infomap().

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

Here is the call graph for this function:

Here is the caller graph for this function:

void TRnd::Save ( TSOut SOut) const
inline

Definition at line 23 of file dt.h.

References TSOut::Save().

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

Here is the call graph for this function:

void TRnd::SaveTxt ( TOLx Lx) const

Definition at line 219 of file dt.cpp.

References TOLx::PutInt(), and Seed.

219  {
220  Lx.PutInt(Seed);
221 }
void PutInt(const TInt &Int)
Definition: lx.h:277
int Seed
Definition: dt.h:16

Here is the call graph for this function:

void TRnd::SaveXml ( TSOut SOut,
const TStr Nm 
) const

Definition at line 14 of file dt.cpp.

References TInt::GetStr(), Seed, and XSaveBETagArg.

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

Here is the call graph for this function:

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.

Referenced by GetNextSeed().

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

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