本文整理汇总了C++中TRnd类的典型用法代码示例。如果您正苦于以下问题:C++ TRnd类的具体用法?C++ TRnd怎么用?C++ TRnd使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TRnd类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main() {
TLSHash LSH(7, 7, DIM, TLSHash::EUCLIDEAN);
LSH.Init();
TRnd Gen;
Gen.Randomize();
TVec<TFltV> DataV;
for (int i=0; i<1000000; i++) {
TFltV Datum;
for (int j=0; j<3; j++) {
Datum.Add(Gen.GetUniDev()*2100);
}
DataV.Add(Datum);
}
LSH.AddV(DataV);
TVec<TPair<TFltV, TFltV> > NeighborsV = LSH.GetAllCandidatePairs();
printf("Number of Candidates: %d\n", NeighborsV.Len());
NeighborsV = LSH.GetAllNearPairs();
printf("Number of Close Pairs: %d\n", NeighborsV.Len());
for (int i=0; i<NeighborsV.Len(); i++) {
outputPoint(NeighborsV[i].GetVal1());
printf(" ");
outputPoint(NeighborsV[i].GetVal2());
printf("\n");
}
return 0;
}
示例2: GenSmallWorld
/// Generates a small-world graph using the Watts-Strogatz model.
/// We assume a circle where each node creates links to NodeOutDeg other nodes.
/// This way at the end each node is connected to 2*NodeOutDeg other nodes.
/// See: Collective dynamics of 'small-world' networks. Watts and Strogatz.
/// URL: http://research.yahoo.com/files/w_s_NATURE_0.pdf
PUNGraph GenSmallWorld(const int& Nodes, const int& NodeOutDeg, const double& RewireProb, TRnd& Rnd) {
THashSet<TIntPr> EdgeSet(Nodes*NodeOutDeg);
IAssertR(Nodes > NodeOutDeg, TStr::Fmt("Insufficient nodes for out degree, %d!", NodeOutDeg));
for (int node = 0; node < Nodes; node++) {
const int src = node;
for (int edge = 1; edge <= NodeOutDeg; edge++) {
int dst = (node+edge) % Nodes; // edge to next neighbor
if (Rnd.GetUniDev() < RewireProb) { // random edge
dst = Rnd.GetUniDevInt(Nodes);
while (dst == src || EdgeSet.IsKey(TIntPr(src, dst))) {
dst = Rnd.GetUniDevInt(Nodes); }
}
EdgeSet.AddKey(TIntPr(src, dst));
}
}
PUNGraph GraphPt = TUNGraph::New();
TUNGraph& Graph = *GraphPt;
Graph.Reserve(Nodes, EdgeSet.Len());
int node;
for (node = 0; node < Nodes; node++) {
IAssert(Graph.AddNode(node) == node);
}
for (int edge = 0; edge < EdgeSet.Len(); edge++) {
Graph.AddEdge(EdgeSet[edge].Val1, EdgeSet[edge].Val2);
}
Graph.Defrag();
return GraphPt;
}
示例3: GenRndBipart
PBPGraph GenRndBipart(const int& LeftNodes, const int& RightNodes, const int& Edges, TRnd& Rnd) {
PBPGraph G = TBPGraph::New();
for (int i = 0; i < LeftNodes; i++) { G->AddNode(i, true); }
for (int i = 0; i < RightNodes; i++) { G->AddNode(LeftNodes+i, false); }
IAssertR(Edges <= LeftNodes*RightNodes, "Too many edges in the bipartite graph!");
for (int edges = 0; edges < Edges; ) {
const int LNId = Rnd.GetUniDevInt(LeftNodes);
const int RNId = LeftNodes + Rnd.GetUniDevInt(RightNodes);
if (G->AddEdge(LNId, RNId) != -2) { edges++; } // is new edge
}
return G;
}
示例4: RndConnectInsideCommunity
void TAGM::RndConnectInsideCommunity(PUNGraph& Graph, const TIntV& CmtyV, const double& Prob, TRnd& Rnd){
int CNodes = CmtyV.Len();
int CEdges = Rnd.GetBinomialDev(Prob,CNodes*(CNodes-1)/2);
THashSet<TIntPr> NewEdgeSet(CEdges);
for (int edge = 0; edge < CEdges; ) {
int SrcNId = CmtyV[Rnd.GetUniDevInt(CNodes)];
int DstNId = CmtyV[Rnd.GetUniDevInt(CNodes)];
if(SrcNId>DstNId){Swap(SrcNId,DstNId);}
if (SrcNId != DstNId && !NewEdgeSet.IsKey(TIntPr(SrcNId,DstNId))) { // is new edge
NewEdgeSet.AddKey(TIntPr(SrcNId,DstNId));
Graph->AddEdge(SrcNId,DstNId);
edge++;
}
}
}
示例5: GetRndNId
int TBPGraph::GetRndNId(TRnd& Rnd) {
const int NNodes = GetNodes();
if (Rnd.GetUniDevInt(NNodes) < GetLNodes()) {
return GetRndLNId(Rnd); }
else {
return GetRndRNId(Rnd); }
}
示例6: choose_seeds
THash<TInt, TInt> * choose_seeds (const PUNGraph g, const int num, const int * infection_state, const int infect) {
THash<TInt, TInt> choices;
THash<TInt, TUNGraph::TNode> nodes;
THash<TInt, TInt> * output = new THash<TInt, TInt> ();
TInt weight = 0;
TInt num_total = 0;
for (TUNGraph::TNodeI n = g->BegNI(); n != g->EndNI(); n++) {
//cout << "nodeID: " << n.GetId() << ",\tStatus: " << infection_state[n.GetId () - 1] << endl;
if (infection_state[n.GetId () - 1] != infect) {
weight += n.GetDeg ();
choices.AddDat (num_total, weight);
nodes.AddDat (num_total, n.GetId());
num_total++;
}
}
// TRnd random ((int) time(NULL));
// TRnd random (0);
TInt num_chosen = 0;
while (num_chosen < num) {
TInt choice = my_random.GetUniDevInt (weight);
TUNGraph::TNode node_choice = nodes[find (choice, choices, 0, num_total-1)];
if (!output->IsKey(node_choice.GetId())) {
num_chosen++;
// cout << node_choice.GetId () << "\n";
output->AddDat(node_choice.GetId (), 1);
}
}
return output;
}
示例7: test
bool test(PGraph &graph, bool followOut, bool followIn) {
printf("\n================================\nFollowOut: %d, FollowIn: %d\n", followOut, followIn);
int iters = 10;
for (int k = 0; k < iters; k++) {
TRnd rnd = TRnd((int)time(0));
int start = graph->GetRndNId(rnd);
rnd.PutSeed(0);
// int target = graph->GetRndNId(rnd);
// printf("Start node: %d, target node: %d\n", start, target);
int target = -1;
printf("Start node: %d\n", start);
struct timeval tv1, tv2;
gettimeofday(&tv1, NULL);
/* Hybrid */
TBreathFS<PGraph> bfs_hybrid(graph, true);
int maxDist_hybrid = bfs_hybrid.DoBfsHybrid(start, followOut, followIn, target);
gettimeofday(&tv2, NULL);
double time_hybrid = timeInSeconds(tv1, tv2);
/* Original */
gettimeofday(&tv1, NULL);
TBreathFS<PGraph> bfs(graph, true);
int maxDist = bfs.DoBfs(start, followOut, followIn, target);
gettimeofday(&tv2, NULL);
double time = timeInSeconds(tv1, tv2);
/* Check results */
if (maxDist_hybrid != maxDist) {
printf("MaxDist incorrect.\n");
return false;
}
if (target == -1) {
if (!checkResults<PGraph>(bfs_hybrid, bfs)) {
printf("NIdDistH values incorrect!\n");
return false;
}
}
printf("Execution times: Original: %.2f, Hybrid: %.2f\n", time, time_hybrid);
}
return true;
}
示例8: InitPosEmb
//Initialize positive embeddings
void InitPosEmb(TIntV& Vocab, int& Dimensions, TRnd& Rnd, TVVec<TFlt, int64>& SynPos) {
SynPos = TVVec<TFlt, int64>(Vocab.Len(),Dimensions);
for (int64 i = 0; i < SynPos.GetXDim(); i++) {
for (int j = 0; j < SynPos.GetYDim(); j++) {
SynPos(i,j) =(Rnd.GetUniDev()-0.5)/Dimensions;
}
}
}
示例9: Init
void TLSHash::Init() {
TRnd Gen;
Gen.Randomize();
for (int i=0; i<Bands*Rows; i++) {
if (Type == JACCARD) {
HashFuncV.Add(TPt<HashFunc>(new JaccardHash(Gen, Dim)));
} else if (Type == COSINE) {
HashFuncV.Add(TPt<HashFunc>(new CosineHash(Gen, Dim)));
} else {
HashFuncV.Add(TPt<HashFunc>(new EuclideanHash(Gen, Dim)));
}
}
for (int i=0; i<Bands; i++) {
SigBucketVHV.Add(THash<TInt, TIntV> (ExpectedSz, true));
}
}
示例10: GenPLSeq
///Generate sequence from Power law
void TAGMUtil::GenPLSeq(TIntV& SzSeq, const int& SeqLen, const double& Alpha, TRnd& Rnd, const int& Min, const int& Max) {
SzSeq.Gen(SeqLen, 0);
while (SzSeq.Len() < SeqLen) {
int Sz = (int) TMath::Round(Rnd.GetPowerDev(Alpha));
if (Sz >= Min && Sz <= Max) {
SzSeq.Add(Sz);
}
}
}
示例11: GenCopyModel
/// Generates a random scale-free network using the Copying Model.
/// The generating process operates as follows: Node u is added to a graph, it
/// selects a random node v, and with prob Beta it links to v, with 1-Beta
/// links u links to neighbor of v. The power-law degree exponent is -1/(1-Beta).
/// See: Stochastic models for the web graph.
/// Kumar, Raghavan, Rajagopalan, Sivakumar, Tomkins, Upfal.
/// URL: http://snap.stanford.edu/class/cs224w-readings/kumar00stochastic.pdf
PNGraph GenCopyModel(const int& Nodes, const double& Beta, TRnd& Rnd) {
PNGraph GraphPt = TNGraph::New();
TNGraph& Graph = *GraphPt;
Graph.Reserve(Nodes, Nodes);
const int startNId = Graph.AddNode();
Graph.AddEdge(startNId, startNId);
for (int n = 1; n < Nodes; n++) {
const int rnd = Graph.GetRndNId();
const int NId = Graph.AddNode();
if (Rnd.GetUniDev() < Beta) {
Graph.AddEdge(NId, rnd); }
else {
const TNGraph::TNodeI NI = Graph.GetNI(rnd);
const int rnd2 = Rnd.GetUniDevInt(NI.GetOutDeg());
Graph.AddEdge(NId, NI.GetOutNId(rnd2));
}
}
return GraphPt;
}
示例12: GetSphereDev
/// Sample random point from the surface of a Dim-dimensional unit sphere.
void GetSphereDev(const int& Dim, TRnd& Rnd, TFltV& ValV) {
if (ValV.Len() != Dim) { ValV.Gen(Dim); }
double Length = 0.0;
for (int i = 0; i < Dim; i++) {
ValV[i] = Rnd.GetNrmDev();
Length += TMath::Sqr(ValV[i]); }
Length = 1.0 / sqrt(Length);
for (int i = 0; i < Dim; i++) {
ValV[i] *= Length;
}
}
示例13: GenConfModel
/// Generates a random undirect graph with a given degree sequence DegSeqV.
/// Configuration model operates as follows. For each node N, of degree
/// DeqSeqV[N] we create DeqSeqV[N] spokes (half-edges). We then pick two
/// spokes at random, and connect the spokes endpoints. We continue this
/// process until no spokes are left. Generally this generates a multigraph
/// (i.e., spokes out of same nodes can be chosen multiple times).We ignore
/// (discard) self-loops and multiple edges. Thus, the generated graph will
/// only approximate follow the given degree sequence. The method is very fast!
PUNGraph GenConfModel(const TIntV& DegSeqV, TRnd& Rnd) {
const int Nodes = DegSeqV.Len();
PUNGraph GraphPt = TUNGraph::New();
TUNGraph& Graph = *GraphPt;
Graph.Reserve(Nodes, -1);
TIntV NIdDegV(DegSeqV.Len(), 0);
int DegSum=0, edges=0;
for (int node = 0; node < Nodes; node++) {
Graph.AddNode(node);
for (int d = 0; d < DegSeqV[node]; d++) { NIdDegV.Add(node); }
DegSum += DegSeqV[node];
}
NIdDegV.Shuffle(Rnd);
TIntPrSet EdgeH(DegSum/2); // set of all edges, is faster than graph edge lookup
if (DegSum % 2 != 0) {
printf("Seg seq is odd [%d]: ", DegSeqV.Len());
for (int d = 0; d < TMath::Mn(100, DegSeqV.Len()); d++) { printf(" %d", (int)DegSeqV[d]); }
printf("\n");
}
int u=0, v=0;
for (int c = 0; NIdDegV.Len() > 1; c++) {
u = Rnd.GetUniDevInt(NIdDegV.Len());
while ((v = Rnd.GetUniDevInt(NIdDegV.Len())) == u) { }
if (u > v) { Swap(u, v); }
const int E1 = NIdDegV[u];
const int E2 = NIdDegV[v];
if (v == NIdDegV.Len()-1) { NIdDegV.DelLast(); }
else { NIdDegV[v] = NIdDegV.Last(); NIdDegV.DelLast(); }
if (u == NIdDegV.Len()-1) { NIdDegV.DelLast(); }
else { NIdDegV[u] = NIdDegV.Last(); NIdDegV.DelLast(); }
if (E1 == E2 || EdgeH.IsKey(TIntPr(E1, E2))) { continue; }
EdgeH.AddKey(TIntPr(E1, E2));
Graph.AddEdge(E1, E2);
edges++;
if (c % (DegSum/100+1) == 0) { printf("\r configuration model: iter %d: edges: %d, left: %d", c, edges, NIdDegV.Len()/2); }
}
printf("\n");
return GraphPt;
}
示例14: GenRndGnm
PGraph GenRndGnm(const int& Nodes, const int& Edges, const bool& IsDir, TRnd& Rnd) {
PGraph GraphPt = PGraph::New();
typename PGraph::TObj& Graph = *GraphPt;
Graph.Reserve(Nodes, Edges);
for (int node = 0; node < Nodes; node++) {
IAssert(Graph.AddNode(node) == node);
}
for (int edge = 0; edge < Edges; ) {
const int SrcNId = Rnd.GetUniDevInt(Nodes);
const int DstNId = Rnd.GetUniDevInt(Nodes);
if (SrcNId != DstNId && Graph.AddEdge(SrcNId, DstNId) != -2) {
if (! IsDir) { Graph.AddEdge(DstNId, SrcNId); }
edge++;
}
}
return GraphPt;
}
示例15: GenGeoPrefAttach
/// Generates a random scale-free graph using the Geometric Preferential
/// Attachment model by Flexman, Frieze and Vera.
/// See: A geometric preferential attachment model of networks by Flexman,
/// Frieze and Vera. WAW 2004.
/// URL: http://math.cmu.edu/~af1p/Texfiles/GeoWeb.pdf
PUNGraph GenGeoPrefAttach(const int& Nodes, const int& OutDeg, const double& Beta, TRnd& Rnd) {
PUNGraph G = TUNGraph::New(Nodes, Nodes*OutDeg);
TFltTrV PointV(Nodes, 0);
TFltV ValV;
// points on a sphere of radius 1/(2*pi)
const double Rad = 0.5 * TMath::Pi;
for (int i = 0; i < Nodes; i++) {
TSnapDetail::GetSphereDev(3, Rnd, ValV);
PointV.Add(TFltTr(Rad*ValV[0], Rad*ValV[1], Rad*ValV[2]));
}
const double R2 = TMath::Sqr(log((double) Nodes) / (pow((double) Nodes, 0.5-Beta)));
TIntV DegV, NIdV;
int SumDeg;
for (int t = 0; t < Nodes; t++) {
const int pid = t;
const TFltTr& P1 = PointV[pid];
// add node
if (! G->IsNode(pid)) { G->AddNode(pid); }
// find neighborhood
DegV.Clr(false); NIdV.Clr(false); SumDeg=0;
for (int p = 0; p < t; p++) {
const TFltTr& P2 = PointV[p];
if (TMath::Sqr(P1.Val1-P2.Val1)+TMath::Sqr(P1.Val2-P2.Val2)+TMath::Sqr(P1.Val3-P2.Val3) < R2) {
NIdV.Add(p);
DegV.Add(G->GetNI(p).GetDeg()+1);
SumDeg += DegV.Last();
}
}
// add edges
for (int m = 0; m < OutDeg; m++) {
const int rnd = Rnd.GetUniDevInt(SumDeg);
int sum = 0, dst = -1;
for (int s = 0; s < DegV.Len(); s++) {
sum += DegV[s];
if (rnd < sum) { dst=s; break; }
}
if (dst != -1) {
G->AddEdge(pid, NIdV[dst]);
SumDeg -= DegV[dst];
NIdV.Del(dst); DegV.Del(dst);
}
}
}
return G;
}