igraph-help
[Top][All Lists]
Advanced

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

Re: [igraph] Odd behavior of srand


From: Gábor Csárdi
Subject: Re: [igraph] Odd behavior of srand
Date: Fri, 13 Mar 2009 13:29:14 +0100

Mark,

this is how to use the same seed from C:

[...]
#include <stdlib.h>
extern int igraph_rng_inited;
int main() {
 srand(1122);
 igraph_rng_inited=1;
[...]

I.e. you must set igraph_rng_inited to 1, otherwise the first igraph
function that uses rand() will call srand(time(0)) before. (And it
also sets igraph_rng_inited to 1, so the subsequent calls work well.)

Gabor

On Fri, Mar 13, 2009 at 1:24 PM, Mark McClure <address@hidden> wrote:
> 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;
> }
>
>
>
> _______________________________________________
> igraph-help mailing list
> address@hidden
> http://lists.nongnu.org/mailman/listinfo/igraph-help
>
>



-- 
Gabor Csardi <address@hidden>     UNIL DGM




reply via email to

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