SNAP Library 2.1, User Reference  2013-09-25 10:47:25
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
TRnd Class Reference

#include <dt.h>

List of all members.

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.

                                              {
    PutSeed(_Seed); Move(Steps);}
TRnd::TRnd ( TSIn SIn) [inline, explicit]

Definition at line 22 of file dt.h.

{SIn.Load(Seed);}

Member Function Documentation

bool TRnd::Check ( )

Definition at line 33 of file dt.cpp.

                {
  int PSeed=Seed; Seed=1;
  for (int SeedN=0; SeedN<10000; SeedN++){GetNextSeed();}
  bool Ok=Seed==1043618065; Seed=PSeed; return Ok;
}
double TRnd::GetBinomialDev ( const double &  Prb,
const int &  Trials 
)

Definition at line 154 of file dt.cpp.

                                                               {
  int j;
  static int nold=(-1);
  double am,em,g,angle,p,bnl,sq,t,y;
  static double pold=(-1.0),pc,plog,pclog,en,oldg;

  p=(Prb <= 0.5 ? Prb : 1.0-Prb);
  am=Trials*p;
  if (Trials < 25) {
    bnl=0.0;
    for (j=1;j<=Trials;j++)
      if (GetUniDev() < p) ++bnl;
  } else if (am < 1.0) {
    g=exp(-am);
    t=1.0;
    for (j=0;j<=Trials;j++) {
      t *= GetUniDev();
      if (t < g) break;
    }
    bnl=(j <= Trials ? j : Trials);
  } else {
    if (Trials != nold) {
      en=Trials;
      oldg=TSpecFunc::LnGamma(en+1.0);
      nold=Trials;
    } if (p != pold) {
      pc=1.0-p;
      plog=log(p);
      pclog=log(pc);
      pold=p;
    }
    sq=sqrt(2.0*am*pc);
    do {
      do {
        angle=TMath::Pi*GetUniDev();
        y=tan(angle);
        em=sq*y+am;
      } while (em < 0.0 || em >= (en+1.0));
      em=floor(em);
      t=1.2*sq*(1.0+y*y)*exp(oldg-(em+1.0)
        -TSpecFunc::LnGamma(en-em+1.0)+em*plog+(en-em)*pclog);
    } while (GetUniDev() > t);
    bnl=em;
  }
  if (p != Prb) bnl=Trials-bnl;
  return bnl;
}
double TRnd::GetExpDev ( )

Definition at line 83 of file dt.cpp.

                      {
  double UniDev;
  do {
    UniDev=GetUniDev();
  } while (UniDev==0.0);
  return -log(UniDev);
}
double TRnd::GetExpDev ( const double &  Lambda)

Definition at line 91 of file dt.cpp.

                                           {
  return GetExpDev()/Lambda;
}
static double TRnd::GetExpDevStep ( const int &  Seed,
const int &  Steps 
) [inline, static]

Definition at line 68 of file dt.h.

                                                                {
    TRnd Rnd(Seed); Rnd.Move(Steps); return Rnd.GetExpDev();}
double TRnd::GetGammaDev ( const int &  Order)

Definition at line 95 of file dt.cpp.

                                        {
  int j;
  double am,e,s,v1,v2,x,y;
  if (Order<1){Fail;}
  if (Order<6) {
    x=1.0;
    for (j=1;j<=Order;j++) x *=GetUniDev();
    x = -log(x);
  } else {
    do {
      do {
        do {
          v1=2.0*GetUniDev()-1.0;
          v2=2.0*GetUniDev()-1.0;
        } while (v1*v1+v2*v2 > 1.0);
        y=v2/v1;
        am=Order-1;
        s=sqrt(2.0*am+1.0);
        x=s*y+am;
      } while (x <= 0.0);
      e=(1.0+y*y)*exp(am*log(x/am)-s*y);
    } while (GetUniDev()>e);
  }
  return x;
}
int TRnd::GetGeoDev ( const double &  Prb) [inline]

Definition at line 45 of file dt.h.

                                  {
    return 1+(int)floor(log(1.0-GetUniDev())/log(1.0-Prb));}
int TRnd::GetNextSeed ( ) [inline, private]

Definition at line 17 of file dt.h.

                   {
    if ((Seed=a*(Seed%q)-r*(Seed/q))>0){return Seed;} else {return Seed+=m;}}
double TRnd::GetNrmDev ( )

Definition at line 63 of file dt.cpp.

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

Definition at line 75 of file dt.cpp.

                                                                            {
  double Val=Mean+GetNrmDev()*SDev;
  if (Val<Mn){Val=Mn;}
  if (Val>Mx){Val=Mx;}
  return Val;
}
static double TRnd::GetNrmDevStep ( const int &  Seed,
const int &  Steps 
) [inline, static]

Definition at line 66 of file dt.h.

                                                                {
    TRnd Rnd(Seed); Rnd.Move(Steps); return Rnd.GetNrmDev();}
double TRnd::GetPoissonDev ( const double &  Mean)

Definition at line 121 of file dt.cpp.

                                            {
  static double sq,alxm,g,oldm=(-1.0);
  double em,t,y;
  if (Mean < 12.0) {
    if (Mean != oldm) {
      oldm=Mean;
      g=exp(-Mean);
    }
    em = -1;
    t=1.0;
    do {
      ++em;
      t *= GetUniDev();
    } while (t>g);
  } else {
    if (Mean != oldm) {
      oldm=Mean;
      sq=sqrt(2.0*Mean);
      alxm=log(Mean);
      g=Mean*alxm-TSpecFunc::LnGamma(Mean+1.0);
    }
    do {
      do {
        y=tan(TMath::Pi*GetUniDev());
        em=sq*y+Mean;
      } while (em < 0.0);
      em=floor(em);
      t=0.9*(1.0+y*y)*exp(em*alxm-TSpecFunc::LnGamma(em+1.0)-g);
    } while (GetUniDev()>t);
  }
  return em;
}
double TRnd::GetPowerDev ( const double &  AlphaSlope) [inline]

Definition at line 47 of file dt.h.

                                              { // power-law degree distribution (AlphaSlope>0)
    IAssert(AlphaSlope>1.0);
    return pow(1.0-GetUniDev(), -1.0/(AlphaSlope-1.0));}
double TRnd::GetRayleigh ( const double &  Sigma) [inline]

Definition at line 50 of file dt.h.

                                          { // 1/sqrt(alpha) = sigma
    IAssert(Sigma>0.0);
    return Sigma*sqrt(-2*log(1-GetUniDev()));}
int TRnd::GetSeed ( ) const [inline]

Definition at line 59 of file dt.h.

{return Seed;}
double TRnd::GetUniDev ( ) [inline]

Definition at line 30 of file dt.h.

{return GetNextSeed()/double(m);}
int TRnd::GetUniDevInt ( const int &  Range = 0)

Definition at line 39 of file dt.cpp.

                                      {
  int Seed=GetNextSeed();
  if (Range==0){return Seed;}
  else {return Seed%Range;}
}
int TRnd::GetUniDevInt ( const int &  MnVal,
const int &  MxVal 
) [inline]

Definition at line 32 of file dt.h.

                                                      {
    IAssert(MnVal<=MxVal); return MnVal+GetUniDevInt(MxVal-MnVal+1);}
int64 TRnd::GetUniDevInt64 ( const int64 Range = 0)

Definition at line 51 of file dt.cpp.

                                            {
  const int64 RndVal = int64((uint64(GetUniDevInt())<<32) | uint64(GetUniDevInt()));
  if (Range==0){return RndVal;}
  else {return RndVal%Range;}
}
static double TRnd::GetUniDevStep ( const int &  Seed,
const int &  Steps 
) [inline, static]

Definition at line 64 of file dt.h.

                                                                {
    TRnd Rnd(Seed); Rnd.Move(Steps); return Rnd.GetUniDev();}
uint TRnd::GetUniDevUInt ( const uint Range = 0)

Definition at line 45 of file dt.cpp.

                                         {
  uint Seed=uint(GetNextSeed()%0x10000)*0x10000+uint(GetNextSeed()%0x10000);
  if (Range==0){return Seed;}
  else {return Seed%Range;}
}
uint64 TRnd::GetUniDevUInt64 ( const uint64 Range = 0)

Definition at line 57 of file dt.cpp.

                                               {
 const uint64 RndVal = uint64((uint64(GetUniDevInt())<<32) | uint64(GetUniDevInt()));
 if (Range==0){return RndVal;}
 else {return RndVal%Range;}
}
double TRnd::GetWeibull ( const double &  K,
const double &  Lambda 
) [inline]

Definition at line 53 of file dt.h.

                                                           { // 1/alpha = lambda
    IAssert(Lambda>0.0 && K>0.0);
    return Lambda*pow(-log(1-GetUniDev()), 1.0/K);}
TRnd TRnd::LoadTxt ( TILx Lx) [static]

Definition at line 215 of file dt.cpp.

                          {
  return TRnd(Lx.GetInt());
}
void TRnd::LoadXml ( const PXmlTok XmlTok,
const TStr Nm 
)

Definition at line 9 of file dt.cpp.

                                                       {
  XLoadHd(Nm);
  Seed=TXmlObjSer::GetIntArg(XmlTok, "Seed");
}
void TRnd::Move ( const int &  Steps)

Definition at line 29 of file dt.cpp.

                               {
  for (int StepN=0; StepN<Steps; StepN++){GetNextSeed();}
}
TRnd& TRnd::operator= ( const TRnd Rnd) [inline]

Definition at line 27 of file dt.h.

{Seed=Rnd.Seed; return *this;}
bool TRnd::operator== ( const TRnd ) const [inline]

Definition at line 28 of file dt.h.

{Fail; return false;}
void TRnd::PutSeed ( const int &  _Seed)

Definition at line 18 of file dt.cpp.

                                  {
  Assert(_Seed>=0);
  if (_Seed==0){
    //Seed=int(time(NULL));
    Seed=abs(int(TSysTm::GetPerfTimerTicks()));
  } else {
    Seed=_Seed;
    //Seed=abs(_Seed*100000)+1;
  }
}
void TRnd::Randomize ( ) [inline]

Definition at line 60 of file dt.h.

void TRnd::Save ( TSOut SOut) const [inline]

Definition at line 23 of file dt.h.

{SOut.Save(Seed);}
void TRnd::SaveTxt ( TOLx Lx) const

Definition at line 219 of file dt.cpp.

                                 {
  Lx.PutInt(Seed);
}
void TRnd::SaveXml ( TSOut SOut,
const TStr Nm 
) const

Definition at line 14 of file dt.cpp.

                                                    {
  XSaveBETagArg(Nm, "Seed", TInt::GetStr(Seed));
}

Member Data Documentation

const int TRnd::a = 16807 [static, private]

Definition at line 15 of file dt.h.

const int TRnd::m = 2147483647 [static, private]

Definition at line 15 of file dt.h.

const int TRnd::q = 127773 [static, private]

Definition at line 15 of file dt.h.

const int TRnd::r = 2836 [static, private]

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: