Index: trunk/SophyaLib/BaseTools/Makefile
===================================================================
--- trunk/SophyaLib/BaseTools/Makefile	(revision 3387)
+++ trunk/SophyaLib/BaseTools/Makefile	(revision 3389)
@@ -1,40 +1,39 @@
 include ../BuildMgr/sophyamake.inc
- 
+
 all: $(SOPHYALIBP)libBaseTools.a
  
 clean:
-	 rm -f $(SOPHYAOBJP)datatype.o $(SOPHYAOBJP)dvlist.o $(SOPHYAOBJP)fiondblock.o $(SOPHYAOBJP)mutyv.o $(SOPHYAOBJP)ndatablock.o $(SOPHYAOBJP)pexceptions.o $(SOPHYAOBJP)ppersist.o $(SOPHYAOBJP)ppfbinstream.o $(SOPHYAOBJP)ppftpointerio.o $(SOPHYAOBJP)rawstream.o $(SOPHYAOBJP)sophyainit.o $(SOPHYAOBJP)timestamp.o $(SOPHYAOBJP)gnumd5.o $(SOPHYAOBJP)srandgen.o
+	 rm -f $(SOPHYAOBJP)datatype.o $(SOPHYAOBJP)dvlist.o $(SOPHYAOBJP)fiondblock.o $(SOPHYAOBJP)mutyv.o $(SOPHYAOBJP)ndatablock.o $(SOPHYAOBJP)pexceptions.o $(SOPHYAOBJP)ppersist.o $(SOPHYAOBJP)ppfbinstream.o $(SOPHYAOBJP)ppftpointerio.o $(SOPHYAOBJP)rawstream.o $(SOPHYAOBJP)sophyainit.o $(SOPHYAOBJP)stsrand.o $(SOPHYAOBJP)timestamp.o $(SOPHYAOBJP)gnumd5.o $(SOPHYAOBJP)srandgen.o
 	rm -f $(SOPHYALIBP)libBaseTools.a
  
-$(SOPHYALIBP)libBaseTools.a : $(SOPHYAOBJP)datatype.o $(SOPHYAOBJP)dvlist.o $(SOPHYAOBJP)fiondblock.o $(SOPHYAOBJP)gnumd5.o $(SOPHYAOBJP)mutyv.o $(SOPHYAOBJP)ndatablock.o $(SOPHYAOBJP)pexceptions.o $(SOPHYAOBJP)ppersist.o $(SOPHYAOBJP)ppfbinstream.o $(SOPHYAOBJP)ppftpointerio.o $(SOPHYAOBJP)rawstream.o $(SOPHYAOBJP)sophyainit.o $(SOPHYAOBJP)srandgen.o $(SOPHYAOBJP)timestamp.o
+$(SOPHYALIBP)libBaseTools.a : $(SOPHYAOBJP)datatype.o $(SOPHYAOBJP)dvlist.o $(SOPHYAOBJP)fiondblock.o $(SOPHYAOBJP)gnumd5.o $(SOPHYAOBJP)mutyv.o $(SOPHYAOBJP)ndatablock.o $(SOPHYAOBJP)pexceptions.o $(SOPHYAOBJP)ppersist.o $(SOPHYAOBJP)ppfbinstream.o $(SOPHYAOBJP)ppftpointerio.o $(SOPHYAOBJP)rawstream.o $(SOPHYAOBJP)sophyainit.o $(SOPHYAOBJP)srandgen.o $(SOPHYAOBJP)stsrand.o $(SOPHYAOBJP)timestamp.o
 	$(AR) $(ARFLAGS) $@ $? 
 	touch $(SOPHYAINCP)/SophyaConfInfo/libsophya.objlist
  
-$(SOPHYAOBJP)datatype.o: datatype.cc sopnamsp.h datatype.h \
+$(SOPHYAOBJP)datatype.o: datatype.cc datatype.h \
   $(SOPHYAINCP)machdefs.h
 	$(CXXCOMPILE) $(CXXTEMPFLG) -o $@  datatype.cc
  
-$(SOPHYAOBJP)dvlist.o: dvlist.cc sopnamsp.h \
-  $(SOPHYAINCP)machdefs.h dvlist.h \
-  objfio.h anydataobj.h ppersist.h pexceptions.h gnumd5.h ppfbinstream.h \
-  rawstream.h ppfnametag.h mutyv.h timestamp.h \
+$(SOPHYAOBJP)dvlist.o: dvlist.cc $(SOPHYAINCP)machdefs.h \
+  dvlist.h objfio.h anydataobj.h ppersist.h pexceptions.h gnumd5.h \
+  ppfbinstream.h rawstream.h ppfnametag.h mutyv.h timestamp.h \
   $(SOPHYAINCP)strutil.h
 	$(CXXCOMPILE) $(CXXTEMPFLG) -o $@  dvlist.cc
  
 $(SOPHYAOBJP)fiondblock.o: fiondblock.cc sopnamsp.h \
-  $(SOPHYAINCP)machdefs.h \
-  pexceptions.h datatype.h fiondblock.h ppersist.h gnumd5.h \
-  ppfbinstream.h rawstream.h ppfnametag.h ndatablock.h anydataobj.h
+  $(SOPHYAINCP)machdefs.h pexceptions.h \
+  datatype.h fiondblock.h ppersist.h gnumd5.h ppfbinstream.h rawstream.h \
+  ppfnametag.h ndatablock.h anydataobj.h
 	$(CXXCOMPILE) $(CXXTEMPFLG) -o $@  fiondblock.cc
  
 $(SOPHYAOBJP)mutyv.o: mutyv.cc sopnamsp.h mutyv.h \
-  $(SOPHYAINCP)machdefs.h \
-  timestamp.h objfio.h anydataobj.h ppersist.h pexceptions.h gnumd5.h \
-  ppfbinstream.h rawstream.h ppfnametag.h
+  $(SOPHYAINCP)machdefs.h timestamp.h objfio.h \
+  anydataobj.h ppersist.h pexceptions.h gnumd5.h ppfbinstream.h \
+  rawstream.h ppfnametag.h
 	$(CXXCOMPILE) $(CXXTEMPFLG) -o $@  mutyv.cc
  
 $(SOPHYAOBJP)ndatablock.o: ndatablock.cc sopnamsp.h \
-  $(SOPHYAINCP)machdefs.h \
-  pexceptions.h ndatablock.h anydataobj.h
+  $(SOPHYAINCP)machdefs.h pexceptions.h \
+  ndatablock.h anydataobj.h thsafeop.h
 	$(CXXCOMPILE) $(CXXTEMPFLG) -o $@  ndatablock.cc
  
@@ -44,46 +43,48 @@
  
 $(SOPHYAOBJP)ppersist.o: ppersist.cc sopnamsp.h \
-  $(SOPHYAINCP)machdefs.h \
-  pexceptions.h ppersist.h gnumd5.h ppfbinstream.h rawstream.h \
-  ppfnametag.h anydataobj.h
+  $(SOPHYAINCP)machdefs.h pexceptions.h \
+  ppersist.h gnumd5.h ppfbinstream.h rawstream.h ppfnametag.h \
+  anydataobj.h
 	$(CXXCOMPILE) $(CXXTEMPFLG) -o $@  ppersist.cc
  
 $(SOPHYAOBJP)ppfbinstream.o: ppfbinstream.cc sopnamsp.h \
-  $(SOPHYAINCP)machdefs.h \
-  ppfbinstream.h rawstream.h pexceptions.h
+  $(SOPHYAINCP)machdefs.h ppfbinstream.h \
+  rawstream.h pexceptions.h
 	$(CXXCOMPILE) $(CXXTEMPFLG) -o $@  ppfbinstream.cc
  
 $(SOPHYAOBJP)ppftpointerio.o: ppftpointerio.cc \
-  $(SOPHYAINCP)machdefs.h \
-  sopnamsp.h ppftpointerio.h ppersist.h pexceptions.h gnumd5.h \
-  ppfbinstream.h rawstream.h ppfnametag.h
+  $(SOPHYAINCP)machdefs.h ppftpointerio.h \
+  ppersist.h pexceptions.h gnumd5.h ppfbinstream.h rawstream.h \
+  ppfnametag.h
 	$(CXXCOMPILE) $(CXXTEMPFLG) -o $@  ppftpointerio.cc
  
 $(SOPHYAOBJP)rawstream.o: rawstream.cc sopnamsp.h rawstream.h \
-  $(SOPHYAINCP)machdefs.h \
-  pexceptions.h
+  $(SOPHYAINCP)machdefs.h pexceptions.h
 	$(CXXCOMPILE) $(CXXTEMPFLG) -o $@  rawstream.cc
  
 $(SOPHYAOBJP)sophyainit.o: sophyainit.cc sopnamsp.h \
-  $(SOPHYAINCP)machdefs.h \
-  sophyainit.h pexceptions.h ppersist.h gnumd5.h ppfbinstream.h \
-  rawstream.h ppfnametag.h fiondblock.h ndatablock.h anydataobj.h \
-  dvlist.h objfio.h mutyv.h timestamp.h fiosegdb.h segdatablock.h \
+  $(SOPHYAINCP)machdefs.h sophyainit.h \
+  pexceptions.h ppersist.h gnumd5.h ppfbinstream.h rawstream.h \
+  ppfnametag.h fiondblock.h ndatablock.h anydataobj.h dvlist.h objfio.h \
+  mutyv.h timestamp.h fiosegdb.h segdatablock.h thsafeop.h \
   ppftpointerio.h ppfwrapstlv.h sversion.h
 	$(CXXCOMPILE) $(CXXTEMPFLG) -o $@  sophyainit.cc
  
-$(SOPHYAOBJP)timestamp.o: timestamp.cc sopnamsp.h \
-  $(SOPHYAINCP)machdefs.h \
+$(SOPHYAOBJP)stsrand.o: stsrand.cc stsrand.h \
+  $(SOPHYAINCP)machdefs.h objfio.h anydataobj.h \
+  ppersist.h pexceptions.h gnumd5.h ppfbinstream.h rawstream.h \
+  ppfnametag.h ndatablock.h thsafeop.h srandgen.h fiondblock.h
+	$(CXXCOMPILE) $(CXXTEMPFLG) -o $@  stsrand.cc
+ 
+$(SOPHYAOBJP)timestamp.o: timestamp.cc $(SOPHYAINCP)machdefs.h \
   timestamp.h objfio.h anydataobj.h ppersist.h pexceptions.h gnumd5.h \
   ppfbinstream.h rawstream.h ppfnametag.h
 	$(CXXCOMPILE) $(CXXTEMPFLG) -o $@  timestamp.cc
  
-$(SOPHYAOBJP)gnumd5.o: gnumd5.c \
-  $(SOPHYAINCP)machdefs.h \
+$(SOPHYAOBJP)gnumd5.o: gnumd5.c $(SOPHYAINCP)machdefs.h \
   bithelp.h gnumd5.h
 	$(CCOMPILE) -o $@  gnumd5.c
  
-$(SOPHYAOBJP)srandgen.o: srandgen.c \
-  $(SOPHYAINCP)machdefs.h \
+$(SOPHYAOBJP)srandgen.o: srandgen.c $(SOPHYAINCP)machdefs.h \
   srandgen.h smathconst.h
 	$(CCOMPILE) -o $@  srandgen.c
Index: trunk/SophyaLib/BaseTools/objlist.list
===================================================================
--- trunk/SophyaLib/BaseTools/objlist.list	(revision 3387)
+++ trunk/SophyaLib/BaseTools/objlist.list	(revision 3389)
@@ -12,3 +12,4 @@
 sophyainit.o
 srandgen.o
+stsrand.o
 timestamp.o
Index: trunk/SophyaLib/BaseTools/smakefile
===================================================================
--- trunk/SophyaLib/BaseTools/smakefile	(revision 3387)
+++ trunk/SophyaLib/BaseTools/smakefile	(revision 3389)
@@ -4,37 +4,36 @@
  
 clean:
-	 rm -f $(SOPHYAOBJP)datatype.o $(SOPHYAOBJP)dvlist.o $(SOPHYAOBJP)fiondblock.o $(SOPHYAOBJP)mutyv.o $(SOPHYAOBJP)ndatablock.o $(SOPHYAOBJP)pexceptions.o $(SOPHYAOBJP)ppersist.o $(SOPHYAOBJP)ppfbinstream.o $(SOPHYAOBJP)ppftpointerio.o $(SOPHYAOBJP)rawstream.o $(SOPHYAOBJP)sophyainit.o $(SOPHYAOBJP)timestamp.o $(SOPHYAOBJP)gnumd5.o $(SOPHYAOBJP)srandgen.o
+	 rm -f $(SOPHYAOBJP)datatype.o $(SOPHYAOBJP)dvlist.o $(SOPHYAOBJP)fiondblock.o $(SOPHYAOBJP)mutyv.o $(SOPHYAOBJP)ndatablock.o $(SOPHYAOBJP)pexceptions.o $(SOPHYAOBJP)ppersist.o $(SOPHYAOBJP)ppfbinstream.o $(SOPHYAOBJP)ppftpointerio.o $(SOPHYAOBJP)rawstream.o $(SOPHYAOBJP)sophyainit.o $(SOPHYAOBJP)stsrand.o $(SOPHYAOBJP)timestamp.o $(SOPHYAOBJP)gnumd5.o $(SOPHYAOBJP)srandgen.o
 	rm -f $(SOPHYALIBP)libBaseTools.a
  
-$(SOPHYALIBP)libBaseTools.a : $(SOPHYAOBJP)datatype.o $(SOPHYAOBJP)dvlist.o $(SOPHYAOBJP)fiondblock.o $(SOPHYAOBJP)gnumd5.o $(SOPHYAOBJP)mutyv.o $(SOPHYAOBJP)ndatablock.o $(SOPHYAOBJP)pexceptions.o $(SOPHYAOBJP)ppersist.o $(SOPHYAOBJP)ppfbinstream.o $(SOPHYAOBJP)ppftpointerio.o $(SOPHYAOBJP)rawstream.o $(SOPHYAOBJP)sophyainit.o $(SOPHYAOBJP)srandgen.o $(SOPHYAOBJP)timestamp.o
+$(SOPHYALIBP)libBaseTools.a : $(SOPHYAOBJP)datatype.o $(SOPHYAOBJP)dvlist.o $(SOPHYAOBJP)fiondblock.o $(SOPHYAOBJP)gnumd5.o $(SOPHYAOBJP)mutyv.o $(SOPHYAOBJP)ndatablock.o $(SOPHYAOBJP)pexceptions.o $(SOPHYAOBJP)ppersist.o $(SOPHYAOBJP)ppfbinstream.o $(SOPHYAOBJP)ppftpointerio.o $(SOPHYAOBJP)rawstream.o $(SOPHYAOBJP)sophyainit.o $(SOPHYAOBJP)srandgen.o $(SOPHYAOBJP)stsrand.o $(SOPHYAOBJP)timestamp.o
 	$(AR) $(ARFLAGS) $@ $? 
 	touch $(SOPHYAINCP)/SophyaConfInfo/libsophya.objlist
  
-$(SOPHYAOBJP)datatype.o: datatype.cc sopnamsp.h datatype.h \
+$(SOPHYAOBJP)datatype.o: datatype.cc datatype.h \
   $(SOPHYAINCP)machdefs.h
 	$(CXXCOMPILE) $(CXXTEMPFLG) -o $@  datatype.cc
  
-$(SOPHYAOBJP)dvlist.o: dvlist.cc sopnamsp.h \
-  $(SOPHYAINCP)machdefs.h dvlist.h \
-  objfio.h anydataobj.h ppersist.h pexceptions.h gnumd5.h ppfbinstream.h \
-  rawstream.h ppfnametag.h mutyv.h timestamp.h \
+$(SOPHYAOBJP)dvlist.o: dvlist.cc $(SOPHYAINCP)machdefs.h \
+  dvlist.h objfio.h anydataobj.h ppersist.h pexceptions.h gnumd5.h \
+  ppfbinstream.h rawstream.h ppfnametag.h mutyv.h timestamp.h \
   $(SOPHYAINCP)strutil.h
 	$(CXXCOMPILE) $(CXXTEMPFLG) -o $@  dvlist.cc
  
 $(SOPHYAOBJP)fiondblock.o: fiondblock.cc sopnamsp.h \
-  $(SOPHYAINCP)machdefs.h \
-  pexceptions.h datatype.h fiondblock.h ppersist.h gnumd5.h \
-  ppfbinstream.h rawstream.h ppfnametag.h ndatablock.h anydataobj.h
+  $(SOPHYAINCP)machdefs.h pexceptions.h \
+  datatype.h fiondblock.h ppersist.h gnumd5.h ppfbinstream.h rawstream.h \
+  ppfnametag.h ndatablock.h anydataobj.h
 	$(CXXCOMPILE) $(CXXTEMPFLG) -o $@  fiondblock.cc
  
 $(SOPHYAOBJP)mutyv.o: mutyv.cc sopnamsp.h mutyv.h \
-  $(SOPHYAINCP)machdefs.h \
-  timestamp.h objfio.h anydataobj.h ppersist.h pexceptions.h gnumd5.h \
-  ppfbinstream.h rawstream.h ppfnametag.h
+  $(SOPHYAINCP)machdefs.h timestamp.h objfio.h \
+  anydataobj.h ppersist.h pexceptions.h gnumd5.h ppfbinstream.h \
+  rawstream.h ppfnametag.h
 	$(CXXCOMPILE) $(CXXTEMPFLG) -o $@  mutyv.cc
  
 $(SOPHYAOBJP)ndatablock.o: ndatablock.cc sopnamsp.h \
-  $(SOPHYAINCP)machdefs.h \
-  pexceptions.h ndatablock.h anydataobj.h
+  $(SOPHYAINCP)machdefs.h pexceptions.h \
+  ndatablock.h anydataobj.h thsafeop.h
 	$(CXXCOMPILE) $(CXXTEMPFLG) -o $@  ndatablock.cc
  
@@ -44,46 +43,48 @@
  
 $(SOPHYAOBJP)ppersist.o: ppersist.cc sopnamsp.h \
-  $(SOPHYAINCP)machdefs.h \
-  pexceptions.h ppersist.h gnumd5.h ppfbinstream.h rawstream.h \
-  ppfnametag.h anydataobj.h
+  $(SOPHYAINCP)machdefs.h pexceptions.h \
+  ppersist.h gnumd5.h ppfbinstream.h rawstream.h ppfnametag.h \
+  anydataobj.h
 	$(CXXCOMPILE) $(CXXTEMPFLG) -o $@  ppersist.cc
  
 $(SOPHYAOBJP)ppfbinstream.o: ppfbinstream.cc sopnamsp.h \
-  $(SOPHYAINCP)machdefs.h \
-  ppfbinstream.h rawstream.h pexceptions.h
+  $(SOPHYAINCP)machdefs.h ppfbinstream.h \
+  rawstream.h pexceptions.h
 	$(CXXCOMPILE) $(CXXTEMPFLG) -o $@  ppfbinstream.cc
  
 $(SOPHYAOBJP)ppftpointerio.o: ppftpointerio.cc \
-  $(SOPHYAINCP)machdefs.h \
-  sopnamsp.h ppftpointerio.h ppersist.h pexceptions.h gnumd5.h \
-  ppfbinstream.h rawstream.h ppfnametag.h
+  $(SOPHYAINCP)machdefs.h ppftpointerio.h \
+  ppersist.h pexceptions.h gnumd5.h ppfbinstream.h rawstream.h \
+  ppfnametag.h
 	$(CXXCOMPILE) $(CXXTEMPFLG) -o $@  ppftpointerio.cc
  
 $(SOPHYAOBJP)rawstream.o: rawstream.cc sopnamsp.h rawstream.h \
-  $(SOPHYAINCP)machdefs.h \
-  pexceptions.h
+  $(SOPHYAINCP)machdefs.h pexceptions.h
 	$(CXXCOMPILE) $(CXXTEMPFLG) -o $@  rawstream.cc
  
 $(SOPHYAOBJP)sophyainit.o: sophyainit.cc sopnamsp.h \
-  $(SOPHYAINCP)machdefs.h \
-  sophyainit.h pexceptions.h ppersist.h gnumd5.h ppfbinstream.h \
-  rawstream.h ppfnametag.h fiondblock.h ndatablock.h anydataobj.h \
-  dvlist.h objfio.h mutyv.h timestamp.h fiosegdb.h segdatablock.h \
+  $(SOPHYAINCP)machdefs.h sophyainit.h \
+  pexceptions.h ppersist.h gnumd5.h ppfbinstream.h rawstream.h \
+  ppfnametag.h fiondblock.h ndatablock.h anydataobj.h dvlist.h objfio.h \
+  mutyv.h timestamp.h fiosegdb.h segdatablock.h thsafeop.h \
   ppftpointerio.h ppfwrapstlv.h sversion.h
 	$(CXXCOMPILE) $(CXXTEMPFLG) -o $@  sophyainit.cc
  
-$(SOPHYAOBJP)timestamp.o: timestamp.cc sopnamsp.h \
-  $(SOPHYAINCP)machdefs.h \
+$(SOPHYAOBJP)stsrand.o: stsrand.cc stsrand.h \
+  $(SOPHYAINCP)machdefs.h objfio.h anydataobj.h \
+  ppersist.h pexceptions.h gnumd5.h ppfbinstream.h rawstream.h \
+  ppfnametag.h ndatablock.h thsafeop.h srandgen.h fiondblock.h
+	$(CXXCOMPILE) $(CXXTEMPFLG) -o $@  stsrand.cc
+ 
+$(SOPHYAOBJP)timestamp.o: timestamp.cc $(SOPHYAINCP)machdefs.h \
   timestamp.h objfio.h anydataobj.h ppersist.h pexceptions.h gnumd5.h \
   ppfbinstream.h rawstream.h ppfnametag.h
 	$(CXXCOMPILE) $(CXXTEMPFLG) -o $@  timestamp.cc
  
-$(SOPHYAOBJP)gnumd5.o: gnumd5.c \
-  $(SOPHYAINCP)machdefs.h \
+$(SOPHYAOBJP)gnumd5.o: gnumd5.c $(SOPHYAINCP)machdefs.h \
   bithelp.h gnumd5.h
 	$(CCOMPILE) -o $@  gnumd5.c
  
-$(SOPHYAOBJP)srandgen.o: srandgen.c \
-  $(SOPHYAINCP)machdefs.h \
+$(SOPHYAOBJP)srandgen.o: srandgen.c $(SOPHYAINCP)machdefs.h \
   srandgen.h smathconst.h
 	$(CCOMPILE) -o $@  srandgen.c
Index: trunk/SophyaLib/BaseTools/srandgen.h
===================================================================
--- trunk/SophyaLib/BaseTools/srandgen.h	(revision 3387)
+++ trunk/SophyaLib/BaseTools/srandgen.h	(revision 3389)
@@ -84,23 +84,4 @@
 #endif
 
-/* -- Declaration d'une classe pour la centralier la doc -- Oct 2006 */
-#ifdef __cplusplus
-namespace SOPHYA {
-class RandomGenerator {
- public:
-  /*! \brief Automatic initialization through Auto_Ini_Ranf() */
-  static inline void AutoInit(int lp=0) { Auto_Ini_Ranf(lp); return; } 
-  /*! \brief Return a random number \b r with a flat distribution  0<=r<=1 */ 
-  static inline double Flat01() { return drand48(); }
-  /*! \brief Return a random number \b r with a flat distribution  -1<=r<=1 */ 
-  static inline double Flatpm1() { return drandpm1(); }
-  /*! \brief Return a random number following a zero mean gaussian distribution  */ 
-  static inline double Gaussian(double sigma=1.) { return GauRnd(0., sigma); }
-  /*! \brief Return a random number following a Poisson distribution with mean \b mu  */ 
-  static inline int    Poisson(double mu) { return PoissRand(mu); }
-};
-} /* namespace SOPHYA */
 #endif
 
-#endif
-
Index: trunk/SophyaLib/BaseTools/stsrand.cc
===================================================================
--- trunk/SophyaLib/BaseTools/stsrand.cc	(revision 3389)
+++ trunk/SophyaLib/BaseTools/stsrand.cc	(revision 3389)
@@ -0,0 +1,225 @@
+#include "stsrand.h"
+#include "thsafeop.h"
+#include "srandgen.h"
+#include "fiondblock.h"
+#include <math.h> 
+
+namespace SOPHYA {
+
+/*!
+   \class RandomGenerator
+   \ingroup BaseTools
+   This class is a thread-safe random number generator.
+   Its PPF handler can be used to save the complete state of the class and the underlying
+   random number generator used.
+
+   \sa SOPHYA::ObjFileIO<RandomGenerator>
+
+*/
+
+// Objet statique global pour gestion de lock entre threads
+static ThSafeOp* ths_rand = NULL;
+
+RandomGenerator::RandomGenerator(size_t n, bool tsafe) 
+{
+  if (ths_rand == NULL)  ths_rand = new ThSafeOp; 
+  if (tsafe) {   // thread-safe
+    fg_nothrsafe = false;
+    if (n < 1) n = 1024;
+    rseq_.ReSize(n, false);
+    idx_ = n;
+  }
+  else {   // NOT thread-safe
+    fg_nothrsafe = true;
+    idx_ = 1;
+  }
+}
+
+RandomGenerator::RandomGenerator(RandomGenerator const & rg)
+{
+  if (ths_rand == NULL)  ths_rand = new ThSafeOp; 
+  if (!rg.fg_nothrsafe) { // thread-safe
+    fg_nothrsafe = false;
+    rseq_.ReSize(rg.rseq_.Size(), false);
+    idx_ = rseq_.Size();
+  }
+  else {   // NOT thread-safe
+    fg_nothrsafe = true;
+    idx_ = 1;
+  }
+}
+ 
+
+RandomGenerator::~RandomGenerator(void) 
+{
+  // rien a faire 
+}
+
+void RandomGenerator::AutoInit(int lp)
+{
+  if (ths_rand == NULL)  ths_rand = new ThSafeOp; 
+  ths_rand->lock();
+  if(lp) cout << "RandomGenerator::AutoInit() : Calling Auto_Ini_Ranf() ..." << endl;
+  Auto_Ini_Ranf(lp);  // Faut-il faire copier/coller du code Auto_Ini_Ranf() ici ? 
+  ths_rand->unlock();
+}
+
+void RandomGenerator::Init(long seed_val, int lp)
+{
+  if (ths_rand == NULL)  ths_rand = new ThSafeOp; 
+  if(lp) cout << "RandomGenerator::Init(long seed=" << seed_val << ")" << endl;
+  ths_rand->lock();
+  srand48(seed_val);
+  ths_rand->unlock();
+  return;
+}
+
+void RandomGenerator::Init(unsigned short seed_16v[3], int lp)
+{
+  if (ths_rand == NULL)  ths_rand = new ThSafeOp; 
+  if(lp) cout << "RandomGenerator::Init(u_short seed_16v[3]=" << seed_16v[0]
+	      << "," << seed_16v[1] << "," << seed_16v[2] << ")" << endl;
+  ths_rand->lock();
+  Init_P(seed_16v);
+  ths_rand->unlock();
+}
+
+void RandomGenerator::GetSeed(unsigned short seed_16v[3], int lp)
+{
+  if (ths_rand == NULL)  ths_rand = new ThSafeOp; 
+  ths_rand->lock();
+  GetSeed_P(seed_16v);
+  ths_rand->unlock();
+  if(lp) cout << "RandomGenerator::GetSeed(u_short seed_16v[3]=" << seed_16v[0]
+	      << "," << seed_16v[1] << "," << seed_16v[2] << ")" << endl;
+  return;
+}
+
+void RandomGenerator::Init_P(unsigned short seed_16v[3])
+{
+  seed48(seed_16v);
+}
+
+void RandomGenerator::GetSeed_P(unsigned short seed_16v[3])
+{
+  unsigned short seed[3] = {0,0,0};
+  unsigned short *p;
+  p = seed48(seed);
+  memcpy(seed_16v,p,3*sizeof(unsigned short));
+  /* on re-initialise a ce qui etait avant */
+  seed48(seed_16v);
+  return;
+}
+
+r_8 RandomGenerator::Gaussian() 
+{
+  r_8 A=Next(); 
+  while (A==0.) A=Next(); 
+  return sqrt(-2.*log(A))*cos(2.*M_PI*Next());
+}
+
+
+uint_8 RandomGenerator::Poisson(double mu,double mumax)
+{
+  double pp,ppi,x;
+
+  if((mumax>0.)&&(mu>=mumax)) {
+    pp = sqrt(mu);
+    while( (x=pp*Gaussian()) < -mu );
+    return (uint_8)(mu+x+0.5);
+  }
+  else {
+    uint_8 n;
+    ppi = pp = exp(-mu);
+    x = Next();
+    n = 0;
+    while (x > ppi) {
+      n++;
+      pp = mu*pp/(double)n;
+      ppi += pp;
+    }
+    return n;
+  }
+  return 0;  // pas necessaire ?
+}
+
+void RandomGenerator::GenSeq(void)
+{
+  ths_rand->lock();
+  for(size_t k=0; k<rseq_.Size(); k++)  rseq_(k) = drand48();
+  ths_rand->unlock();
+  idx_ = 0;
+}
+
+//----------------------------------------------------------
+// Classe pour la gestion de persistance
+// ObjFileIO<RandomGenerator>
+//----------------------------------------------------------
+
+/* --Methode-- */
+DECL_TEMP_SPEC  /* equivalent a template <> , pour SGI-CC en particulier */
+void        ObjFileIO<RandomGenerator>::WriteSelf(POutPersist& s) const
+{
+  if (dobj == NULL)
+    throw NullPtrError("ObjFileIO<RandomGenerator>::WriteSelf() dobj=NULL");
+  ths_rand->lock();  // thread-safety
+  uint_4 itab[6];
+  //itab : [0]: version, [1,2,3] = srand48 state/seed  , [4,5] = reserved for future use
+  itab[0] = 1;
+  // On recupere et on ecrit ds le PPF l'etat du generateur aleatoire
+  unsigned short seed_16v[3];
+  dobj->GetSeed_P(seed_16v);
+  for(int i=0; i<3; i++)  itab[i+1] = seed_16v[i];
+  itab[4] = 0;
+  s.Put(itab, 6);
+  uint_8 sz = dobj->rseq_.Size();
+  s.Put(sz);  // Taille du tableau intermediaire
+  uint_8 ix = dobj->idx_;
+  s.Put(ix);  // valeur de l'index 
+
+  if (dobj->rseq_.Size() > 0)  s << dobj->rseq_;  // On ecrit le tableau (NDataBlock) si necessaire
+  ths_rand->unlock(); // thread-safety
+  return;
+}
+
+/* --Methode-- */
+DECL_TEMP_SPEC  /* equivalent a template <> , pour SGI-CC en particulier */
+void        ObjFileIO<RandomGenerator>::ReadSelf(PInPersist& s)
+{
+  uint_4 itab[6];
+  //itab : [0]: version, [1,2,3] = srand48 state/seed  , [4] = reserved for future use
+  s.Get(itab, 6);
+  uint_8 sz,ix;
+  s.Get(sz);  // Taille du tableau intermediaire
+  s.Get(ix);  // Taille du tableau intermediaire
+
+  if (dobj == NULL) dobj = new RandomGenerator(sz, (sz>0)?true:false);
+  dobj->idx_ = ix;
+  if (sz > 0) { 
+    s >> dobj->rseq_;  // On lit le tableau (NDataBlock) si necessaire
+    dobj->fg_nothrsafe = false;
+  }
+  else {  // Objet lu est NON thread-safe, taille_tableau rseq_ = 0
+    dobj->fg_nothrsafe = true;
+    if (dobj->rseq_.Size() > 0)  dobj->rseq_.Dealloc(); 
+  }
+  // On initialise l'etat du generateur aleatoire avec les valeurs lues
+  unsigned short seed_16v[3];
+  dobj->GetSeed_P(seed_16v);
+  for(int i=0; i<3; i++)  seed_16v[i] = itab[i+1];
+  dobj->Init(seed_16v, 0);
+  return;
+}
+
+// ---------------------------------------------------------
+#ifdef __CXX_PRAGMA_TEMPLATES__
+#pragma define_template ObjFileIO<RandomGenerator>
+#endif
+
+#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
+template class ObjFileIO<RandomGenerator>;
+#endif
+// ---------------------------------------------------------
+
+}  /* namespace SOPHYA */
+
Index: trunk/SophyaLib/BaseTools/stsrand.h
===================================================================
--- trunk/SophyaLib/BaseTools/stsrand.h	(revision 3389)
+++ trunk/SophyaLib/BaseTools/stsrand.h	(revision 3389)
@@ -0,0 +1,101 @@
+#ifndef STSRAND_H_SEEN
+#define STSRAND_H_SEEN
+
+// Classe RandomGenerator 
+// Generateur aleatoire compatible multi-thread
+//
+// R. Ansari          (C) UPS+LAL IN2P3/CNRS  2007
+// C. Magneville      (C) DAPNIA/SPP  CEA     2007
+
+
+#include "machdefs.h"
+#include "objfio.h"   // Pour rendre la classe PPersist
+
+#include "ndatablock.h"
+
+namespace SOPHYA {
+
+class RandomGenerator : public AnyDataObj {
+
+ public:
+  RandomGenerator(size_t n = 1024, bool tsafe=true);
+  RandomGenerator(RandomGenerator const & rg);
+
+  virtual ~RandomGenerator();
+
+  /*! \brief Automatic initialization using the current time */
+  static  void AutoInit(int lp=0);
+  /*! \brief Initialization using the specified seed  */
+  static  void Init(long seed_val, int lp=0);
+  /*! \brief Initialization using the specified seed  */
+  static  void Init(unsigned short seed_16v[3], int lp=0);
+  /*! \brief Getting the current seed state */
+  static  void GetSeed(unsigned short seed_16v[3], int lp=0);
+
+  /*! \brief Return a random number \b r with a flat distribution  0<=r<1 */ 
+  inline r_8 Flat01() {return Next();}
+
+  /*! \brief Return a random number \b r with a flat distribution  -1<=r<1 */ 
+  inline r_8 Flatpm1() {return 2.*Next()-1.;}
+
+  /*! \brief Return a random number following a gaussian distribution (sigma=1, mean=0*/ 
+  virtual r_8 Gaussian(); 
+
+  /*! \brief Return a random number following a gaussian distribution "sigma", (mean=0)*/ 
+  virtual r_8 Gaussian(double sigma) 
+    { return sigma*Gaussian(); }
+ /*! \brief Return a random number following a gaussian distribution with mean=mu, and sigma */ 
+  virtual r_8 Gaussian(double sigma,double mu) 
+    { return sigma*Gaussian()+mu; }
+
+  /*! \brief alias for Gaussian() */ 
+  inline r_8 NorRand()
+    { return Gaussian(); }
+
+  /*! \brief Return a random number following a poisson distribution with mean mu */ 
+  virtual  uint_8 Poisson(double mu, double mumax);
+  
+  /*! \brief Return a random number following a poisson distribution with mean mu */ 
+  inline uint_8 Poisson(double mu)
+    { return Poisson(mu, -1.); }
+
+   /*! \brief Return a random number following a poisson distribution with mean mu */ 
+  inline uint_8  PoissRandLimit(double mu,double mumax=10.)
+    { return Poisson(mu, mumax); }
+ 
+  //  Pour la gestion de persistance PPF
+  friend class ObjFileIO<RandomGenerator> ;
+
+ protected:
+  // ---- protected data members
+  NDataBlock<r_8>  rseq_;  
+  size_t idx_;
+  bool fg_nothrsafe;  // if true --> NOT thread-safe
+  // ---- protected methods 
+  void GenSeq();
+  inline r_8 Next() 
+    {
+      if (rseq_.Size() == 0)  return drand48(); 
+      else { 
+	if(idx_==rseq_.Size()) GenSeq(); 
+	return(rseq_(idx_++));
+      }
+    }
+  // Non thread-safe version of Init() and GetSeed()
+  static  void Init_P(unsigned short seed_16v[3]);
+  static  void GetSeed_P(unsigned short seed_16v[3]);
+
+};  // Fin de la classe RandomGenerator
+
+// Classe pour la gestion de persistance PPF :  ObjFileIO<RandomGenerator>
+
+/*! Writes the random generator object state in the POutPersist stream \b os */
+inline POutPersist& operator << (POutPersist& os, RandomGenerator & obj)
+{ ObjFileIO<RandomGenerator> fio(&obj);  fio.Write(os);  return(os); }
+/*! Reads the random generator object state from the PInPersist stream \b is */
+inline PInPersist& operator >> (PInPersist& is, RandomGenerator & obj)
+{ ObjFileIO<RandomGenerator> fio(&obj); is.SkipToNextObject(); fio.Read(is); return(is); }
+
+} /* namespace SOPHYA */
+
+#endif 
