source: Sophya/trunk/SophyaLib/BaseTools/srandgen.h@ 3239

Last change on this file since 3239 was 3099, checked in by ansari, 19 years ago

Ajout PoissRand() + classe RandomGenerator / doc ds srandgen.h .cc , Reza 02/11/2006

File size: 3.2 KB
Line 
1/* fonctions pour generateurs aleatoires */
2/* cmv 23/06/94 */
3/* R.Ansari 04/2000 Copie de nbrand.h .c */
4
5#ifndef SRANDGEN_H_SEEN
6#define SRANDGEN_H_SEEN
7
8#include "machdefs.h"
9#include <stdlib.h>
10
11/* ------- SPECIFIQUE Mac Darwin OSX <= 10.2 (gcc < 3.x) -------- */
12#if defined(Darwin) && defined(__GNUC__) && (__GNUC__ < 3)
13#include "osx_values.h"
14/* Declaration de drand48 et srand48 etc , mis ds SysSpec
15 Reza 11/02/2003 */
16#ifdef __cplusplus
17extern "C" {
18#endif
19unsigned short int * seed48(unsigned short int seed16v[3]);
20void srand48(long seedval);
21/* Faute de mieux pour le moment : */
22#ifdef __cplusplus
23}
24#endif
25#define drand48() ((double)(random())/LONG_MAX)
26#endif
27/* ------- FIN SPECIFIQUE Mac OSX / Darwin V <= 10.2 --------- */
28
29#ifdef __cplusplus
30/* Declaration en fonctions inlines pour du C++ */
31inline float frand01() { return ( (float) drand48() ); }
32inline double drand01() { return ( drand48() ); }
33inline double ranf01() { return ( drand48() ); }
34
35inline float frandpm1() { return ( 2. * (float) drand48() - 1.); }
36inline double drandpm1() { return ( 2. * drand48() - 1.); }
37inline double ranfpm1() { return ( 2. * drand48() - 1.); }
38#else
39/* les memes en define pour du C */
40#define frand01() ( (float) drand48() )
41#define drand01() ( drand48() )
42#define ranf01() drand01()
43
44#define frandpm1() ( 2. * frand01() - 1.)
45#define drandpm1() ( 2. * drand01() - 1.)
46#define ranfpm1() drandpm1()
47#endif
48
49struct tirage_alea {
50 int Nbin;
51 double Min,Max,Lbin;
52 double *Tab;
53};
54typedef struct tirage_alea TIREALEA;
55
56#ifdef __cplusplus
57extern "C" {
58#endif
59
60void Ini_Ranf_Quick(long seed_val, int lp);
61void Ini_Ranf(unsigned short seed_16v[3], int lp);
62void Get_Ranf(unsigned short seed_16v[3], int lp);
63void Auto_Ini_Ranf(int lp);
64
65void SetGauRange(double range);
66float NorRand1(void);
67double GauRnd1(double am, double s);
68float NorRand(void);
69double GauRnd(double am, double s);
70int NormCo(double *a,double *b
71 ,double mx,double my,double sx,double sy,double ro);
72void NormGau(double *x,double *y
73 ,double mx,double my,double sa,double sb,double teta);
74
75int PoissRand(double mu);
76
77TIREALEA *init_tirage_alea(int nbin,double xmin,double xmax,double (*fonc) (double));
78double tirage_alea(TIREALEA *alea);
79int end_tirage_alea(TIREALEA *alea);
80
81#ifdef __cplusplus
82}
83#endif
84
85/* -- Declaration d'une classe pour la centralier la doc -- Oct 2006 */
86#ifdef __cplusplus
87namespace SOPHYA {
88class RandomGenerator {
89 public:
90 /*! \brief Automatic initialization through Auto_Ini_Ranf() */
91 static inline void AutoInit(int lp=0) { Auto_Ini_Ranf(lp); return; }
92 /*! \brief Return a random number \b r with a flat distribution 0<=r<=1 */
93 static inline double Flat01() { return drand48(); }
94 /*! \brief Return a random number \b r with a flat distribution -1<=r<=1 */
95 static inline double Flatpm1() { return drandpm1(); }
96 /*! \brief Return a random number following a zero mean gaussian distribution */
97 static inline double Gaussian(double sigma=1.) { return GauRnd(0., sigma); }
98 /*! \brief Return a random number following a Poisson distribution with mean \b mu */
99 static inline int Poisson(double mu) { return PoissRand(mu); }
100};
101} /* namespace SOPHYA */
102#endif
103
104#endif
105
Note: See TracBrowser for help on using the repository browser.