mercredi 5 août 2015

Save and restart random chain (drand48) from checkpoint in C

I'm trying to write a program that gives the same result either if is executed entirely or if is stopped and restarted from some checkpoint. To do that I need to be able to repeat exactly the same random number sequence in any scenario. So, here a piece of code where I tried to do that, but of course, I'm not successful. Could you help me to fix this code?

int main(){
 int i;
 long int seed;

 // Initial seed
 srand48(3);

 // Print 5 random numbers
 for(i=0;i<5;i++)  printf("%d %f\n",i,drand48());

 // CHECKPOINT: HOW TO PROPERLY SET seed?
 seed=mrand48(); // <--- FIXME

 // 5 numbers more
 for(i=5;i<10;i++) printf("%d %f\n",i,drand48());

 // Restart from the CHECKPOINT.
 srand48(seed);

 // Last 5 numbers again
 for(i=5;i<10;i++) printf("%d %f\n",i,drand48());

}




Aucun commentaire:

Enregistrer un commentaire