igraph-help
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[igraph] Odd behavior of srand


From: Mark McClure
Subject: [igraph] Odd behavior of srand
Date: Fri, 13 Mar 2009 08:24:22 -0400

I've noticed some odd behavior when using igraph's random graph
generation tools.  It seems that srand() doesn't affect any
immediately following random graph generators, but it does affect
subsequent calls.  The little program below illustrates. The first
loop generates a sequence of random ERG graphs and prints the
number of edges.  They should all be the same because of the call
to srand in the loop.  In actuality, the last 4 graphs are the
same but the first is almost certainly different from these.  In
the second loop, all the graphs are the same - presumably, due
some srand initialization from the first.  You can comment out the
first loop to see that the same thing happens with GRG graphs as
well.

There is a simple work around; simply call some random graph
generator at the start of your program.

Mark McClure

-------- Random program ----------
#include <igraph.h>
#include <stdlib.h>

int main(void) {
    igraph_t graph;
    igraph_vector_t x, y;
    int i;
   
    for(i=0; i<5; i++) {
        srand(1);
        igraph_erdos_renyi_game(&graph, IGRAPH_ERDOS_RENYI_GNP,
            1000, 0.5, IGRAPH_UNDIRECTED, IGRAPH_NO_LOOPS);
        printf("Number of ERG edges is %d.\n", (int) igraph_ecount(&graph));
    }
    igraph_vector_init(&x, 1000);
    igraph_vector_init(&y, 1000);
    for(i=0; i<5; i++) {
        srand(1);
        igraph_grg_game(&graph, 1000, .1, 1, &x, &y);
        printf("Number of GRG edges is %d.\n", (int) igraph_ecount(&graph));
    }
    igraph_destroy(&graph);
    return 0;
}



reply via email to

[Prev in Thread] Current Thread [Next in Thread]