Index: /trunk/SophyaLib/NTools/Makefile
===================================================================
--- /trunk/SophyaLib/NTools/Makefile	(revision 3599)
+++ /trunk/SophyaLib/NTools/Makefile	(revision 3600)
@@ -1,11 +1,11 @@
 include ../BuildMgr/sophyamake.inc
- 
+
 all: $(SOPHYALIBP)libNTools.a
  
 clean:
-	 rm -f $(SOPHYAOBJP)FSAppIrrSmpl.o $(SOPHYAOBJP)cimage.o $(SOPHYAOBJP)cspline.o $(SOPHYAOBJP)datatypes.o $(SOPHYAOBJP)dates.o $(SOPHYAOBJP)difeq.o $(SOPHYAOBJP)dynccd.o $(SOPHYAOBJP)fct1dfit.o $(SOPHYAOBJP)fct2dfit.o $(SOPHYAOBJP)fftmserver.o $(SOPHYAOBJP)fftpserver.o $(SOPHYAOBJP)fftservintf.o $(SOPHYAOBJP)functab.o $(SOPHYAOBJP)generaldata.o $(SOPHYAOBJP)generalfit.o $(SOPHYAOBJP)generalfunc.o $(SOPHYAOBJP)imageop.o $(SOPHYAOBJP)integ.o $(SOPHYAOBJP)median.o $(SOPHYAOBJP)ntoolsinit.o $(SOPHYAOBJP)objfitter.o $(SOPHYAOBJP)perandom.o $(SOPHYAOBJP)poly.o $(SOPHYAOBJP)rk4cdifeq.o $(SOPHYAOBJP)simplesort.o $(SOPHYAOBJP)simplex.o $(SOPHYAOBJP)slinparbuff.o $(SOPHYAOBJP)tabmath.o $(SOPHYAOBJP)toeplitzMatrix.o $(SOPHYAOBJP)datime.o $(SOPHYAOBJP)fftmayer_r4.o $(SOPHYAOBJP)fftmayer_r8.o $(SOPHYAOBJP)fftpackc.o $(SOPHYAOBJP)matxop.o $(SOPHYAOBJP)nbmath.o $(SOPHYAOBJP)nbtri.o
+	 rm -f $(SOPHYAOBJP)FSAppIrrSmpl.o $(SOPHYAOBJP)cimage.o $(SOPHYAOBJP)cspline.o $(SOPHYAOBJP)datatypes.o $(SOPHYAOBJP)dates.o $(SOPHYAOBJP)difeq.o $(SOPHYAOBJP)dynccd.o $(SOPHYAOBJP)fct1dfit.o $(SOPHYAOBJP)fct2dfit.o $(SOPHYAOBJP)fftmserver.o $(SOPHYAOBJP)fftpserver.o $(SOPHYAOBJP)fftservintf.o $(SOPHYAOBJP)functab.o $(SOPHYAOBJP)generaldata.o $(SOPHYAOBJP)generalfit.o $(SOPHYAOBJP)generalfunc.o $(SOPHYAOBJP)imageop.o $(SOPHYAOBJP)integ.o $(SOPHYAOBJP)median.o $(SOPHYAOBJP)ntoolsinit.o $(SOPHYAOBJP)objfitter.o $(SOPHYAOBJP)perandom.o $(SOPHYAOBJP)poly.o $(SOPHYAOBJP)rk4cdifeq.o $(SOPHYAOBJP)simplesort.o $(SOPHYAOBJP)simplex.o $(SOPHYAOBJP)slinparbuff.o $(SOPHYAOBJP)tabmath.o $(SOPHYAOBJP)toeplitzMatrix.o $(SOPHYAOBJP)tsfunran.o $(SOPHYAOBJP)datime.o $(SOPHYAOBJP)fftmayer_r4.o $(SOPHYAOBJP)fftmayer_r8.o $(SOPHYAOBJP)fftpackc.o $(SOPHYAOBJP)matxop.o $(SOPHYAOBJP)nbmath.o $(SOPHYAOBJP)nbtri.o
 	rm -f $(SOPHYALIBP)libNTools.a
  
-$(SOPHYALIBP)libNTools.a : $(SOPHYAOBJP)cimage.o $(SOPHYAOBJP)cspline.o $(SOPHYAOBJP)datatypes.o $(SOPHYAOBJP)dates.o $(SOPHYAOBJP)datime.o $(SOPHYAOBJP)difeq.o $(SOPHYAOBJP)dynccd.o $(SOPHYAOBJP)fct1dfit.o $(SOPHYAOBJP)fct2dfit.o $(SOPHYAOBJP)fftmayer_r4.o $(SOPHYAOBJP)fftmayer_r8.o $(SOPHYAOBJP)fftmserver.o $(SOPHYAOBJP)fftpackc.o $(SOPHYAOBJP)fftpserver.o $(SOPHYAOBJP)fftservintf.o $(SOPHYAOBJP)FSAppIrrSmpl.o $(SOPHYAOBJP)functab.o $(SOPHYAOBJP)generaldata.o $(SOPHYAOBJP)generalfit.o $(SOPHYAOBJP)generalfunc.o $(SOPHYAOBJP)integ.o $(SOPHYAOBJP)matxop.o $(SOPHYAOBJP)median.o $(SOPHYAOBJP)nbmath.o $(SOPHYAOBJP)nbtri.o $(SOPHYAOBJP)ntoolsinit.o $(SOPHYAOBJP)objfitter.o $(SOPHYAOBJP)perandom.o $(SOPHYAOBJP)poly.o $(SOPHYAOBJP)rk4cdifeq.o $(SOPHYAOBJP)simplesort.o $(SOPHYAOBJP)simplex.o $(SOPHYAOBJP)slinparbuff.o $(SOPHYAOBJP)tabmath.o $(SOPHYAOBJP)toeplitzMatrix.o
+$(SOPHYALIBP)libNTools.a : $(SOPHYAOBJP)cimage.o $(SOPHYAOBJP)cspline.o $(SOPHYAOBJP)datatypes.o $(SOPHYAOBJP)dates.o $(SOPHYAOBJP)datime.o $(SOPHYAOBJP)difeq.o $(SOPHYAOBJP)dynccd.o $(SOPHYAOBJP)fct1dfit.o $(SOPHYAOBJP)fct2dfit.o $(SOPHYAOBJP)fftmayer_r4.o $(SOPHYAOBJP)fftmayer_r8.o $(SOPHYAOBJP)fftmserver.o $(SOPHYAOBJP)fftpackc.o $(SOPHYAOBJP)fftpserver.o $(SOPHYAOBJP)fftservintf.o $(SOPHYAOBJP)FSAppIrrSmpl.o $(SOPHYAOBJP)functab.o $(SOPHYAOBJP)generaldata.o $(SOPHYAOBJP)generalfit.o $(SOPHYAOBJP)generalfunc.o $(SOPHYAOBJP)integ.o $(SOPHYAOBJP)matxop.o $(SOPHYAOBJP)median.o $(SOPHYAOBJP)nbmath.o $(SOPHYAOBJP)nbtri.o $(SOPHYAOBJP)ntoolsinit.o $(SOPHYAOBJP)objfitter.o $(SOPHYAOBJP)perandom.o $(SOPHYAOBJP)poly.o $(SOPHYAOBJP)rk4cdifeq.o $(SOPHYAOBJP)simplesort.o $(SOPHYAOBJP)simplex.o $(SOPHYAOBJP)slinparbuff.o $(SOPHYAOBJP)tabmath.o $(SOPHYAOBJP)toeplitzMatrix.o $(SOPHYAOBJP)tsfunran.o
 	$(AR) $(ARFLAGS) $@ $? 
 	touch $(SOPHYAINCP)/SophyaConfInfo/libsophya.objlist
@@ -691,4 +691,10 @@
 	$(CXXCOMPILE) $(CXXTEMPFLG) -o $@  toeplitzMatrix.cc
  
+$(SOPHYAOBJP)tsfunran.o: tsfunran.cc $(SOPHYAINCP)sopnamsp.h \
+  $(SOPHYAINCP)machdefs.h \
+  $(SOPHYAINCP)pexceptions.h \
+  $(SOPHYAINCP)machdefs.h pemath.h
+	$(CXXCOMPILE) $(CXXTEMPFLG) -o $@  tsfunran.cc
+ 
 $(SOPHYAOBJP)datime.o: datime.c $(SOPHYAINCP)machdefs.h \
   $(SOPHYAINCP)strutil.h nbconst.h datime.h
Index: /trunk/SophyaLib/NTools/objlist.list
===================================================================
--- /trunk/SophyaLib/NTools/objlist.list	(revision 3599)
+++ /trunk/SophyaLib/NTools/objlist.list	(revision 3600)
@@ -34,2 +34,3 @@
 tabmath.o
 toeplitzMatrix.o
+tsfunran.o
Index: /trunk/SophyaLib/NTools/perandom.h
===================================================================
--- /trunk/SophyaLib/NTools/perandom.h	(revision 3599)
+++ /trunk/SophyaLib/NTools/perandom.h	(revision 3600)
@@ -1,5 +1,5 @@
 // This may look like C code, but it is really -*- C++ -*-
 // Nombres aleatoires pour Peida.
-//   NON CE N'EST PAS MON CODE ... C. Magneville   1996-2000
+// C. Magneville   1996-2000
 // DAPNIA/SPP (Saclay) / CEA    LAL - IN2P3/CNRS  (Orsay)
 
Index: /trunk/SophyaLib/NTools/smakefile
===================================================================
--- /trunk/SophyaLib/NTools/smakefile	(revision 3599)
+++ /trunk/SophyaLib/NTools/smakefile	(revision 3600)
@@ -4,8 +4,8 @@
  
 clean:
-	 rm -f $(SOPHYAOBJP)FSAppIrrSmpl.o $(SOPHYAOBJP)cimage.o $(SOPHYAOBJP)cspline.o $(SOPHYAOBJP)datatypes.o $(SOPHYAOBJP)dates.o $(SOPHYAOBJP)difeq.o $(SOPHYAOBJP)dynccd.o $(SOPHYAOBJP)fct1dfit.o $(SOPHYAOBJP)fct2dfit.o $(SOPHYAOBJP)fftmserver.o $(SOPHYAOBJP)fftpserver.o $(SOPHYAOBJP)fftservintf.o $(SOPHYAOBJP)functab.o $(SOPHYAOBJP)generaldata.o $(SOPHYAOBJP)generalfit.o $(SOPHYAOBJP)generalfunc.o $(SOPHYAOBJP)imageop.o $(SOPHYAOBJP)integ.o $(SOPHYAOBJP)median.o $(SOPHYAOBJP)ntoolsinit.o $(SOPHYAOBJP)objfitter.o $(SOPHYAOBJP)perandom.o $(SOPHYAOBJP)poly.o $(SOPHYAOBJP)rk4cdifeq.o $(SOPHYAOBJP)simplesort.o $(SOPHYAOBJP)simplex.o $(SOPHYAOBJP)slinparbuff.o $(SOPHYAOBJP)tabmath.o $(SOPHYAOBJP)toeplitzMatrix.o $(SOPHYAOBJP)datime.o $(SOPHYAOBJP)fftmayer_r4.o $(SOPHYAOBJP)fftmayer_r8.o $(SOPHYAOBJP)fftpackc.o $(SOPHYAOBJP)matxop.o $(SOPHYAOBJP)nbmath.o $(SOPHYAOBJP)nbtri.o
+	 rm -f $(SOPHYAOBJP)FSAppIrrSmpl.o $(SOPHYAOBJP)cimage.o $(SOPHYAOBJP)cspline.o $(SOPHYAOBJP)datatypes.o $(SOPHYAOBJP)dates.o $(SOPHYAOBJP)difeq.o $(SOPHYAOBJP)dynccd.o $(SOPHYAOBJP)fct1dfit.o $(SOPHYAOBJP)fct2dfit.o $(SOPHYAOBJP)fftmserver.o $(SOPHYAOBJP)fftpserver.o $(SOPHYAOBJP)fftservintf.o $(SOPHYAOBJP)functab.o $(SOPHYAOBJP)generaldata.o $(SOPHYAOBJP)generalfit.o $(SOPHYAOBJP)generalfunc.o $(SOPHYAOBJP)imageop.o $(SOPHYAOBJP)integ.o $(SOPHYAOBJP)median.o $(SOPHYAOBJP)ntoolsinit.o $(SOPHYAOBJP)objfitter.o $(SOPHYAOBJP)perandom.o $(SOPHYAOBJP)poly.o $(SOPHYAOBJP)rk4cdifeq.o $(SOPHYAOBJP)simplesort.o $(SOPHYAOBJP)simplex.o $(SOPHYAOBJP)slinparbuff.o $(SOPHYAOBJP)tabmath.o $(SOPHYAOBJP)toeplitzMatrix.o $(SOPHYAOBJP)tsfunran.o $(SOPHYAOBJP)datime.o $(SOPHYAOBJP)fftmayer_r4.o $(SOPHYAOBJP)fftmayer_r8.o $(SOPHYAOBJP)fftpackc.o $(SOPHYAOBJP)matxop.o $(SOPHYAOBJP)nbmath.o $(SOPHYAOBJP)nbtri.o
 	rm -f $(SOPHYALIBP)libNTools.a
  
-$(SOPHYALIBP)libNTools.a : $(SOPHYAOBJP)cimage.o $(SOPHYAOBJP)cspline.o $(SOPHYAOBJP)datatypes.o $(SOPHYAOBJP)dates.o $(SOPHYAOBJP)datime.o $(SOPHYAOBJP)difeq.o $(SOPHYAOBJP)dynccd.o $(SOPHYAOBJP)fct1dfit.o $(SOPHYAOBJP)fct2dfit.o $(SOPHYAOBJP)fftmayer_r4.o $(SOPHYAOBJP)fftmayer_r8.o $(SOPHYAOBJP)fftmserver.o $(SOPHYAOBJP)fftpackc.o $(SOPHYAOBJP)fftpserver.o $(SOPHYAOBJP)fftservintf.o $(SOPHYAOBJP)FSAppIrrSmpl.o $(SOPHYAOBJP)functab.o $(SOPHYAOBJP)generaldata.o $(SOPHYAOBJP)generalfit.o $(SOPHYAOBJP)generalfunc.o $(SOPHYAOBJP)integ.o $(SOPHYAOBJP)matxop.o $(SOPHYAOBJP)median.o $(SOPHYAOBJP)nbmath.o $(SOPHYAOBJP)nbtri.o $(SOPHYAOBJP)ntoolsinit.o $(SOPHYAOBJP)objfitter.o $(SOPHYAOBJP)perandom.o $(SOPHYAOBJP)poly.o $(SOPHYAOBJP)rk4cdifeq.o $(SOPHYAOBJP)simplesort.o $(SOPHYAOBJP)simplex.o $(SOPHYAOBJP)slinparbuff.o $(SOPHYAOBJP)tabmath.o $(SOPHYAOBJP)toeplitzMatrix.o
+$(SOPHYALIBP)libNTools.a : $(SOPHYAOBJP)cimage.o $(SOPHYAOBJP)cspline.o $(SOPHYAOBJP)datatypes.o $(SOPHYAOBJP)dates.o $(SOPHYAOBJP)datime.o $(SOPHYAOBJP)difeq.o $(SOPHYAOBJP)dynccd.o $(SOPHYAOBJP)fct1dfit.o $(SOPHYAOBJP)fct2dfit.o $(SOPHYAOBJP)fftmayer_r4.o $(SOPHYAOBJP)fftmayer_r8.o $(SOPHYAOBJP)fftmserver.o $(SOPHYAOBJP)fftpackc.o $(SOPHYAOBJP)fftpserver.o $(SOPHYAOBJP)fftservintf.o $(SOPHYAOBJP)FSAppIrrSmpl.o $(SOPHYAOBJP)functab.o $(SOPHYAOBJP)generaldata.o $(SOPHYAOBJP)generalfit.o $(SOPHYAOBJP)generalfunc.o $(SOPHYAOBJP)integ.o $(SOPHYAOBJP)matxop.o $(SOPHYAOBJP)median.o $(SOPHYAOBJP)nbmath.o $(SOPHYAOBJP)nbtri.o $(SOPHYAOBJP)ntoolsinit.o $(SOPHYAOBJP)objfitter.o $(SOPHYAOBJP)perandom.o $(SOPHYAOBJP)poly.o $(SOPHYAOBJP)rk4cdifeq.o $(SOPHYAOBJP)simplesort.o $(SOPHYAOBJP)simplex.o $(SOPHYAOBJP)slinparbuff.o $(SOPHYAOBJP)tabmath.o $(SOPHYAOBJP)toeplitzMatrix.o $(SOPHYAOBJP)tsfunran.o
 	$(AR) $(ARFLAGS) $@ $? 
 	touch $(SOPHYAINCP)/SophyaConfInfo/libsophya.objlist
@@ -691,4 +691,10 @@
 	$(CXXCOMPILE) $(CXXTEMPFLG) -o $@  toeplitzMatrix.cc
  
+$(SOPHYAOBJP)tsfunran.o: tsfunran.cc $(SOPHYAINCP)sopnamsp.h \
+  $(SOPHYAINCP)machdefs.h \
+  $(SOPHYAINCP)pexceptions.h \
+  $(SOPHYAINCP)machdefs.h pemath.h
+	$(CXXCOMPILE) $(CXXTEMPFLG) -o $@  tsfunran.cc
+ 
 $(SOPHYAOBJP)datime.o: datime.c $(SOPHYAINCP)machdefs.h \
   $(SOPHYAINCP)strutil.h nbconst.h datime.h
Index: /trunk/SophyaLib/NTools/tsfunran.cc
===================================================================
--- /trunk/SophyaLib/NTools/tsfunran.cc	(revision 3600)
+++ /trunk/SophyaLib/NTools/tsfunran.cc	(revision 3600)
@@ -0,0 +1,228 @@
+#include "sopnamsp.h"
+#include "machdefs.h"
+#include "pexceptions.h"
+#include "tsfunran.h"
+#include <iostream>
+
+
+////////////////////////////////////////////////////////////////////////////
+/*!
+  \class SOPHYA::TsFunRan
+  \ingroup NTools
+  Classe for generating random variables from 1D function
+*/
+
+
+/********* Methode *********/
+/*! Creator from a function.
+\verbatim
+ - if pdf==true: f est une densite de probabilite (PDF)
+                 non necessairement normalisee
+   if pdf==false: f est  une fonction de distribution (DF).
+                  non necessairement normalisee
+ - Le tirage aleatoire est fait sur un histogramme
+   Histo(xMin,xMax,nBin) (voir convention dans Histo).
+ - Chaque bin de l'histogramme contient la valeur de la PDF
+   (ou de la DF) au centre du bin: h(i)=f(BinCenter(i))
+ - Les valeurs retournees sont les valeurs du centre des bins
+   pour le tirage non interpole et toutes les valeurs
+   entre [xmin,xmax] pour le tirage interpole
+ - La pdf doit etre interpretee comme etant nulle
+   pour des x<=xmin et x>=xmax
+ - Dans le bin "i" entre [x1,x2[ et de centre x0, h(i)=pdf(x0).
+   Pour le tirage interpole, la DF est approximee par un segment
+   et pdf(x0) est l'exces de proba entre x1 et x2:
+   bin 0 entre [xmin,BinHighEdge(0)[ :
+               la pdf va de 0 a pdf(BinCenter(0))
+   bin 1 entre [BinLowEdge(1),BinHighEdge(1)[:
+               la pdf va de pdf(BinCenter(0)) a pdf(BinCenter(1))
+   ...
+   bin n-1 entre [BinLowEdge(n-1),xmax[:
+               la pdf va de pdf(BinCenter(n-2)) a pdf(BinCenter(n-1))
+\endverbatim
+*/
+TsFunRan::TsFunRan(TsFunRan::Func f, r_8 xMin, r_8 xMax, int_4 nBin, bool pdf)
+  : Histo(xMin,xMax,nBin)
+{
+ if(nBin<=1)
+   throw RangeCheckError("TsFunRan::TsFunRan less than 2 bins requested");
+ for(int_4 i=0;i<nBin;i++) (*this)(i) = f(BinCenter(i));
+ rg_ = new RandomGenerator(1,false);
+ create_DF(pdf);
+}
+
+/********* Methode *********/
+/*! Creator from a ClassFunc
+See TsFunRan::TsFunRan(TsFunRan::Func...) for further comments.
+*/
+TsFunRan::TsFunRan(ClassFunc& f, r_8 xMin, r_8 xMax, int_4 nBin, bool pdf)
+  : Histo(xMin,xMax,nBin)
+{
+ if(nBin<=1)
+   throw RangeCheckError("TsFunRan::TsFunRan less than 2 bins requested");
+ for(int_4 i=0;i<nBin;i++) (*this)(i) = f(BinCenter(i));
+ rg_ = new RandomGenerator(1,false);
+ create_DF(pdf);
+}
+
+/********* Methode *********/
+/*! Creator from an table.
+If pdf=true, tab is a probability density fonction (not necessary normalised).
+If pdf=false, tab is a distribution function (not necessarly normalized to 1).
+The return random values will be between 0 and nBin-1.
+See TsFunRan::TsFunRan(TsFunRan::Func...) for further comments.
+*/
+TsFunRan::TsFunRan(r_8 *tab, int_4 nBin, bool pdf)
+: Histo(-0.5,nBin-0.5,nBin)
+{
+ if(nBin<=1)
+   throw RangeCheckError("TsFunRan::TsFunRan less than 2 bins requested");
+ for(int_4 i=0;i<nBin;i++) (*this)(i) = tab[i];
+ rg_ = new RandomGenerator(1,false);
+ create_DF(pdf);
+}
+
+/********* Methode *********/
+/*! Creator from an table.
+If pdf=true, tab is a probability density fonction (not necessary normalised).
+If pdf=false, tab is a distribution function (not necessarly normalized to 1).
+The content of tab is identified has the content of
+an Histogram define by Histo(xMin,xMax,nBin).
+See FunRan::TsFunRan(TsFunRan::Func...) for further comments.
+*/
+TsFunRan::TsFunRan(r_8 *tab, int_4 nBin, r_8 xMin, r_8 xMax, bool pdf)
+: Histo(xMin,xMax,nBin)
+{
+ if(nBin<=1)
+   throw RangeCheckError("TsFunRan::TsFunRan less than 2 bins requested");
+ for(int_4 i=0;i<nBin;i++) (*this)(i) = tab[i];
+ rg_ = new RandomGenerator(1,false);
+ create_DF(pdf);
+}
+
+/********* Methode *********/
+/*! Creator from a TVector
+*/
+TsFunRan::TsFunRan(TVector<r_8>& tab, int_4 nBin, bool pdf)
+: Histo(-0.5,nBin-0.5,nBin)
+{
+ if(nBin<=1)
+   throw RangeCheckError("TsFunRan::TsFunRan less than 2 bins requested");
+ for(int_4 i=0;i<nBin;i++) (*this)(i) = tab(i);
+ rg_ = new RandomGenerator(1,false);
+ create_DF(pdf);
+}
+
+/********* Methode *********/
+/*! Creator from a TVector
+*/
+TsFunRan::TsFunRan(TVector<r_8>& tab, int_4 nBin, r_8 xMin, r_8 xMax, bool pdf)
+: Histo(xMin,xMax,nBin)
+{
+ if(nBin<=1)
+   throw RangeCheckError("TsFunRan::TsFunRan less than 2 bins requested");
+ for(int_4 i=0;i<nBin;i++) (*this)(i) = tab(i);
+ rg_ = new RandomGenerator(1,false);
+ create_DF(pdf);
+}
+
+/********* Methode *********/
+/*! Creator from an histogram
+If pdf=true, h is a probability density fonction (not necessary normalised).
+If pdf=false, h is a distribution function (not necessarly normalized to 1).
+See TsFunRan::TsFunRan(TsFunRan::Func...) for further comments.
+*/
+TsFunRan::TsFunRan(Histo &h, bool pdf)
+  : Histo(h)
+{
+ if(mBins<=1)
+   throw RangeCheckError("TsFunRan::TsFunRan less than 2 bins requested");
+ rg_ = new RandomGenerator(1,false);
+ create_DF(pdf);
+}
+
+/********* Methode *********/
+/*! Creator by copy */
+TsFunRan::TsFunRan(const TsFunRan& fh)
+  : Histo(fh)
+{
+  rg_ = new RandomGenerator(*fh.rg_);
+}
+
+/********* Methode *********/
+/*! Creator by default */
+TsFunRan::TsFunRan(void)
+{
+  rg_ = new RandomGenerator(1,false);
+}
+
+/********* Methode *********/
+TsFunRan::~TsFunRan(void)
+{
+  if(rg_!=NULL) delete rg_;
+}
+
+/********* Methode *********/
+void TsFunRan::create_DF(bool pdf)
+// Creation (si necessaire) et normalisation de la DF
+{
+  // On fabrique la FD si necessaire
+  if(pdf)
+    for(int_4 i=1;i<mBins;i++) (*this)(i) += (*this)(i-1);
+
+  // On normalise la FD
+  if((*this)(mBins-1)<=0.) {
+    cout<<"TsFunRan::TsFunRan(Histo) not a distribution function last bin is <=0"<<endl;
+    throw RangeCheckError("TsFunRan::TsFunRan(Histo) not a distribution function last bin is <=0");
+  }
+ for(int_4 i=0;i<mBins;i++) (*this)(i) /= (*this)(mBins-1);
+}
+
+/********* Methode *********/
+void TsFunRan::SetRandomGenerator(RandomGenerator& rg)
+{
+  if(rg_!=NULL) delete rg_;
+  rg_ = new RandomGenerator(rg);
+}
+
+/********* Methode *********/
+/*! Tirage avec retour du numero de bin entre 0 et mBins-1.
+It returns the first bin whose content is greater or equal
+to the random uniform number (in [0,1[)
+*/
+int_4 TsFunRan::BinRandom()
+{
+ // recherche du premier bin plus grand ou egal a z
+ r_8 z=rg_->Flat01();
+ for(int_4 i=0;i<mBins;i++) if(z<(*this)(i)) return i;
+ return mBins-1;
+}
+
+/********* Methode *********/
+/*! Tirage avec retour abscisse du bin non interpole. */
+r_8 TsFunRan::Random()
+{
+ r_8 z=rg_->Flat01();
+ int ibin = mBins-1;
+ for(int_4 i=0;i<mBins;i++) if(z<(*this)(i)) {ibin=i; break;}
+ return BinCenter(ibin);
+}
+
+/********* Methode *********/
+/*! Tirage avec retour abscisse du bin interpole. */
+r_8 TsFunRan::RandomInterp(void)
+{
+ r_8 z=rg_->Flat01();
+ int ibin = mBins-1;
+ r_8 z1=0., z2;
+ for(int_4 i=0;i<mBins;i++) {
+   z2 = (*this)(i);
+   if(z<z2) {ibin=i; break;}
+   z1 = z2;
+ }
+
+ // l'algorithme garanti que "z2-z1 != 0" et "z1<z2"
+ return BinLowEdge(ibin) + (z-z1)/(z2-z1)*binWidth;
+ 
+}
+
Index: /trunk/SophyaLib/NTools/tsfunran.h
===================================================================
--- /trunk/SophyaLib/NTools/tsfunran.h	(revision 3600)
+++ /trunk/SophyaLib/NTools/tsfunran.h	(revision 3600)
@@ -0,0 +1,45 @@
+// This may look like C code, but it is really -*- C++ -*-
+// Tirages aleatoires sur une fonction (thread safe)
+// C. Magneville   2009
+// DAPNIA/SPP (Saclay) / CEA    LAL - IN2P3/CNRS  (Orsay)
+
+#ifndef TSFUNRAN_SEEN
+#define TSFUNRAN_SEEN
+
+#include "machdefs.h"
+#include <stdlib.h>
+#include <math.h>
+#include "histos.h"
+#include "tvector.h"
+#include "stsrand.h"
+#include "classfunc.h"
+
+namespace SOPHYA {  
+
+class TsFunRan : public Histo {
+public:
+  typedef r_8 (*Func)(r_8);
+  TsFunRan(ClassFunc& f, r_8 xMin=0.0, r_8 xMax=1.0, int_4 nBin=100, bool pdf=true);
+  TsFunRan(Func f, r_8 xMin=0.0, r_8 xMax=1.0, int_4 nBin=100, bool pdf=true);
+  TsFunRan(r_8 *tab, int_4 nBin, bool pdf=true);
+  TsFunRan(r_8 *tab, int_4 nBin, r_8 xMin, r_8 xMax, bool pdf=true);
+  TsFunRan(TVector<r_8>& tab, int_4 nBin, bool pdf=true);
+  TsFunRan(TVector<r_8>& tab, int_4 nBin, r_8 xMin, r_8 xMax, bool pdf=true);
+  TsFunRan(Histo &h, bool pdf=true);
+  TsFunRan(const TsFunRan& fh);
+  TsFunRan(void);
+  virtual ~TsFunRan(void);
+
+  void SetRandomGenerator(RandomGenerator& rg);
+
+  int_4 BinRandom(void);
+  r_8 Random(void);
+  r_8 RandomInterp(void);
+protected:
+  void create_DF(bool pdf);
+  RandomGenerator* rg_;
+};
+
+} // namespace SOPHYA
+
+#endif
