1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
18 Revision 1.2 1999/09/29 09:24:28 fca
19 Introduction of the Copyright and cvs Log
23 ///////////////////////////////////////////////////////////////////////////
25 // Generate universal random numbers on all common machines.
26 // Available distributions : Uniform, Gaussian, Poisson and
27 // User defined function
32 // 2) Same sequence of 24-bit real numbers on all common machines
36 // G.Marsaglia and A.Zaman, FSU-SCRI-87-50, Florida State University, 1987.
41 // Float_t rndm; // Variable to hold a single random number
42 // const Int_t n=1000;
43 // Float_t rvec[n]; // Vector to hold n random numbers
45 // AliRandom r; // Create a Random object with default sequence
47 // rndm=r.Uniform(); // Provide a uniform random number in <0,1>
50 // rndm=r.Uniform(a,b); // Provide a uniform random number in <a,b>
51 // r.Uniform(rvec,n); // Provide n uniform randoms in <0,1> in rvec
52 // r.Uniform(rvec,n,a,b); // Provide n uniform randoms in <a,b> in rvec
54 // rndm=r.Gauss(); // Provide a Gaussian random number with
55 // // mean=0 and sigma=1
58 // rndm=r.Gauss(mean,sigma); // Provide a Gaussian random number
59 // // with specified mean and sigma
60 // r.Gauss(rvec,n); // n Gaussian randoms mean=0 sigma=1
61 // r.Gauss(rvec,n,mean,sigma); // n Gaussian randoms with specified
64 // rndm=r.Poisson(mean); // Provide a Poisson random number with
66 // r.Poisson(rvec,nmean); // n Poisson randoms with specified mean
69 // AliRandom p(seed); // Create a Random object with specified seed.
70 // // The sequence is started from scratch.
73 // AliRandom q(seed,cnt1,cnt2); // Create a Random object with specified seed
74 // // The sequence is started at the location
75 // // denoted by the counters cnt1 and cnt2.
77 // q.Info(); // Print the current seed, cnt1 and cnt2 values.
78 // q.GetSeed(); // Provide the current seed value.
79 // q.GetCnt1(); // Provide the current cnt1 value.
80 // q.GetCnt2(); // Provide the current cnt2 value.
82 // Float_t udist(Float_t x) // A user defined distribution
88 // q.SetUser(a,b,nbins,udist); // Initialise generator for udist distribution
89 // q.User(); // Provide a random number according to the udist distribution
90 // q.User(rvec,n); // Provide n randoms according to the udist distribution
92 // Float_t* x=new Float_t[nbins];
93 // Float_t* y=new Float_t[nbins];
95 // ... code to fill x[] and y[] ..
98 // s.SetUser(x,y,nbins); // Initialise generator for (x[i],y[i]) distribution
99 // s.User(); // Provide a random number according to the user distribution
100 // s.User(rvec,n); // Provide n randoms according to the user distribution
104 // 1) Allowed seed values : 0 <= seed <= 921350143
105 // Default seed = 53310452
106 // 2) To ensure a unique sequence for each run, one can automatically
107 // construct a seed value by e.g. using the date and time.
108 // 3) Using the rvec facility saves a lot of CPU time for large n values.
110 //--- Author: Nick van Eijndhoven 11-oct-1997 UU-SAP Utrecht
111 ///////////////////////////////////////////////////////////////////////////
113 #include "AliRandom.h"
115 ClassImp(AliRandom) // Class implementation to enable ROOT I/O
117 AliRandom::AliRandom()
119 // Creation of an AliRandom object and default initialisation.
121 // A seed is used to create the initial u[97] table.
122 // This seed is converted into four startup parameters i j k and l
123 // (see member function "unpack").
125 // Suggested test values : i=12 j=34 k=56 l=78 (see article)
126 // which corresponds to : seed = 53310452
128 Int_t seed=53310452; // Default seed
129 Start(seed,0,0); // Start the sequence for this seed from scratch
131 ///////////////////////////////////////////////////////////////////////////
132 AliRandom::AliRandom(Int_t seed)
134 // Creation of an AliRandom object and user defined initialisation
136 Start(seed,0,0); // Start the sequence for this seed from scratch
138 ///////////////////////////////////////////////////////////////////////////
139 AliRandom::AliRandom(Int_t seed,Int_t cnt1,Int_t cnt2)
141 // Creation of an AliRandom object and user defined initialisation
143 // seed is the seed to create the initial u[97] table.
144 // The range of the seed is : 0 <= seed <= 921350143
146 // cnt1 and cnt2 are the parameters for the counting system
147 // to enable a start of the sequence at a certain point.
148 // The current values of seed, cnt1 and cnt2 can be obtained
149 // via the member functions "GetSeed", "GetCnt1" and "GetCnt2" resp.
150 // To start from scratch one should select : cnt1=0 and cnt2=0
152 Start(seed,cnt1,cnt2); // Start the sequence from a user defined point
154 ///////////////////////////////////////////////////////////////////////////
155 AliRandom::~AliRandom()
157 // Destructor to delete memory allocated for the area function arrays
158 if (fXa) delete [] fXa;
160 if (fYa) delete [] fYa;
162 if (fIbins) delete [] fIbins;
165 ///////////////////////////////////////////////////////////////////////////
166 void AliRandom::Start(Int_t seed,Int_t cnt1,Int_t cnt2)
168 // Start a certain sequence from scratch or from a user defined point
170 // The algorithm to start from scratch is based on the routine RSTART
171 // as described in the report by G.Marsaglia and A.Zaman
172 // (FSU-SCRI-87-50 Florida State University 1987).
174 // seed is the seed to create the initial u[97] table.
175 // This seed is converted into four startup parameters i j k and l
176 // (see the member function "unpack").
178 // The range of the seed is : 0 <= seed <= 921350143
180 // Suggested test values : i=12 j=34 k=56 l=78 (see article)
181 // which corresponds to : seed = 53310452
183 // cnt1 and cnt2 are the parameters for the counting system
184 // to enable a start of the sequence at a certain point.
185 // The current values of seed, cnt1 and cnt2 can be obtained
186 // via the member functions "GetSeed", "GetCnt1" and "GetCnt2" resp.
187 // To start from scratch one should select : cnt1=0 and cnt2=0
189 // Reset the area function
195 // Clipping parameter to prevent overflow of the counting system
198 // Set the lags for the Fibonacci sequence of the first part
199 // The sequence is set to F(97,33,*) (see article)
203 // Unpack the seed value and determine i, j, k and l
206 Unpack(seed,i,j,k,l);
212 // Fill the starting table for the first part of the combination
215 for (Int_t ii=0; ii<97; ii++)
220 for (Int_t jj=1; jj<=24; jj++)
222 m=(((i*j)%179)*k)%179;
227 if ((l*m)%64 >= 32) s+=t;
233 // Initialise the second part of the combination
234 fC=362436./16777216.;
235 fCd=7654321./16777216.;
236 fCm=16777213./16777216.;
238 // Generate random numbers upto the user selected starting point
239 // on basis of the counting system
240 if (cnt1 > 0) Uniform(cnt1);
243 for (Int_t n=1; n<=cnt2; n++)
249 ///////////////////////////////////////////////////////////////////////////
250 void AliRandom::Unpack(Int_t seed,Int_t& i,Int_t& j,Int_t& k,Int_t& l)
252 // Unpack the seed into the four startup parameters i,j,k and l
254 // The range of the seed is : 0 <= seed <= 921350143
256 // From the article :
257 // The i,j and k values may be chosen in the interval : 1 <= n <= 178
258 // The l value may be in the interval : 0 <= l <= 168
260 // Taking into account the period of the 3-lagged Fibonacci sequence
261 // The following "bad" combinations have to be ruled out :
273 // To rule these "bad" combinations out all together, we choose
274 // the following allowed ranges :
275 // The i,j and k values may be chosen in the interval : 2 <= n <= 177
276 // The l value may be in the interval : 0 <= l <= 168
278 // and use the formula :
279 // seed = (i-2)*176*176*169 + (j-2)*176*169 + (k-2)*169 + l
281 if ((seed >= 0) && (seed <= 921350143)) // Check seed value
284 Int_t imin2=idum/(176*176*169);
285 idum=idum%(176*176*169);
286 Int_t jmin2=idum/(176*169);
288 Int_t kmin2=idum/169;
297 cout << " *AliRandom::unpack()* Unallowed seed value encountered."
298 << " seed = " << seed << endl;
299 cout << " Seed will be set to default value." << endl;
301 seed=53310452; // Default seed
302 Start(seed,0,0); // Start the sequence for this seed from scratch
305 ///////////////////////////////////////////////////////////////////////////
306 Int_t AliRandom::GetSeed()
308 // Provide the current seed value
311 ///////////////////////////////////////////////////////////////////////////
312 Int_t AliRandom::GetCnt1()
314 // Provide the current value of the counter cnt1
317 ///////////////////////////////////////////////////////////////////////////
318 Int_t AliRandom::GetCnt2()
320 // Provide the current value of the counter cnt2
323 ///////////////////////////////////////////////////////////////////////////
324 void AliRandom::Info()
326 // Print the current seed, cnt1 and cnt2 values
327 cout << " *Random* seed = " << fSeed
328 << " cnt1 = " << fCnt1 << " cnt2 = " << fCnt2 << endl;
330 ///////////////////////////////////////////////////////////////////////////
331 Float_t AliRandom::Uniform()
333 // Generate uniform random numbers in the interval <0,1>
335 // The algorithm is based on lagged Fibonacci sequences (first part)
336 // combined with a congruential method (second part)
337 // as described in the report by G.Marsaglia and A.Zaman
338 // (FSU-SCRI-87-50 Florida State University 1987).
341 // 1) Period = 2**144
342 // 2) Same sequence of 24-bit real numbers on all common machines
344 // First part of the combination : F(97,33,*) (see article for explanation)
345 Float_t unirnu=fU[fI-1]-fU[fJ-1];
346 if (unirnu < 0) unirnu+=1.;
353 // Second part of the combination (see article for explanation)
355 if (fC < 0.) fC+=fCm;
357 if (unirnu < 0.) unirnu+=1.;
359 // Update the counting system to enable sequence continuation
360 // at an arbitrary starting position.
361 // Two counters have been introduced to avoid overflow
362 // fCnt1 : Counter which goes up to fClip
363 // and is reset when fClip is reached
364 // fCnt2 : Counts the number of times fClip has been reached
372 if (unirnu <= 0.) unirnu=Uniform(); // Exclude 0. from the range
376 ///////////////////////////////////////////////////////////////////////////
377 Float_t AliRandom::Uniform(Float_t a,Float_t b)
379 // Generate uniform random numbers in the interval <a,b>
383 Float_t rndm=Uniform();
384 rndm=rmin+fabs(rndm*(a-b));
388 ///////////////////////////////////////////////////////////////////////////
389 void AliRandom::Uniform(Float_t* vec,Int_t n,Float_t a,Float_t b)
391 // Generate a vector of uniform random numbers in the interval <a,b>
392 // This saves lots of (member)function calls in case many random
393 // numbers are needed at once.
395 // n = The number of random numbers to be generated
397 // The algorithm is based on lagged Fibonacci sequences (first part)
398 // combined with a congruential method (second part)
399 // as described in the report by G.Marsaglia and A.Zaman
400 // (FSU-SCRI-87-50 Florida State University 1987).
403 // 1) Period = 2**144
404 // 2) Same sequence of 24-bit real numbers on all common machines
406 // Determine the minimum of a and b
410 // First generate random numbers within <0,1>
411 if (n > 0) // Check n value
413 for (Int_t jvec=0; jvec<n; jvec++)
415 // First part of the combination : F(97,33,*)
416 Float_t unirnu=fU[fI-1]-fU[fJ-1];
417 if (unirnu < 0) unirnu+=1.;
424 // Second part of the combination
426 if (fC < 0.) fC+=fCm;
428 if (unirnu < 0.) unirnu+=1.;
430 // Update the counting system to enable sequence continuation
431 // at an arbitrary starting position.
432 // Two counters have been introduced to avoid overflow
433 // fCnt1 : Counter which goes up to fClip
434 // and is reset when fClip is reached
435 // fCnt2 : Counts the number of times fClip has been reached
443 if (unirnu <= 0.) unirnu=Uniform(); // Exclude 0. from the range
445 // Fill the vector within the selected range
446 vec[jvec]=rmin+fabs(unirnu*(a-b));
451 cout << " *AliRandom::Uniform* Invalid value n = " << n << endl;
454 ///////////////////////////////////////////////////////////////////////////
455 void AliRandom::Uniform(Float_t* vec,Int_t n)
457 // Generate a vector of uniform random numbers in the interval <0,1>
458 // This saves lots of (member)function calls in case many random
459 // numbers are needed at once.
461 // n = The number of random numbers to be generated
463 Uniform(vec,n,0.,1.);
465 ///////////////////////////////////////////////////////////////////////////
466 void AliRandom::Uniform(Int_t n)
468 // Generate n uniform random numbers in in one go.
469 // This saves lots of (member)function calls in case one needs to skip
470 // to a certain point in a sequence.
472 // n = The number of random numbers to be generated
474 // Note : No check is made here to exclude 0 from the range.
475 // It's only the number of generated randoms that counts
477 // The algorithm is based on lagged Fibonacci sequences (first part)
478 // combined with a congruential method (second part)
479 // as described in the report by G.Marsaglia and A.Zaman
480 // (FSU-SCRI-87-50 Florida State University 1987).
483 // 1) Period = 2**144
484 // 2) Same sequence of 24-bit real numbers on all common machines
486 if (n > 0) // Check n value
488 for (Int_t jvec=0; jvec<n; jvec++)
490 // First part of the combination : F(97,33,*)
491 Float_t unirnu=fU[fI-1]-fU[fJ-1];
492 if (unirnu < 0) unirnu+=1.;
499 // Second part of the combination
501 if (fC < 0.) fC+=fCm;
503 if (unirnu < 0.) unirnu+=1.;
505 // Update the counting system to enable sequence continuation
506 // at an arbitrary starting position.
507 // Two counters have been introduced to avoid overflow
508 // fCnt1 : Counter which goes up to fClip
509 // and is reset when fClip is reached
510 // fCnt2 : Counts the number of times fClip has been reached
521 cout << " *AliRandom::Uniform* Invalid value n = " << n << endl;
524 ///////////////////////////////////////////////////////////////////////////
525 Float_t AliRandom::Gauss(Float_t mean,Float_t sigma)
527 // Generate gaussian distributed random numbers with certain mean and sigma
530 // P(x) = The gaussian distribution function
531 // --> ln(P) provides an expression for (x-xmean)**2 from which
532 // the following expression for x can be obtained
534 // x = xmean +/- sigma * sqrt(-2*ln(q))
536 // in which q is an expression in terms of pi, sigma and p and lies within
537 // the interval <0,1>.
539 // The gaussian distributed x values are obtained as follows :
541 // 1) Two uniform random numbers q1 and q2 in <0,1> are generated.
542 // 2) q1 is in fact a uniform generated value for P which is substituted
543 // directly in the formula above.
544 // 3) The value of q2 determines whether we use the + or - sign.
546 // Generate the two uniform random numbers q1 and q2 in <0,1>
551 // Use q1 and q2 to get the gaussian distributed random number
552 Float_t pi=acos(-1.);
553 Float_t unirng=mean+cos(2.*pi*q2)*sigma*sqrt(-2.*log(q1));
557 ///////////////////////////////////////////////////////////////////////////
558 Float_t AliRandom::Gauss()
560 // Generate gaussian distributed random numbers with mean=0 and sigma=1
564 ///////////////////////////////////////////////////////////////////////////
565 void AliRandom::Gauss(Float_t* vec,Int_t n,Float_t mean,Float_t sigma)
567 // Generate a vector of gaussian random numbers with certain mean and sigma
568 // This saves lots of (member)function calls in case many random
569 // numbers are needed at once.
571 // n = The number of random numbers to be generated
573 if (n > 0) // Check n value
575 // The vector to hold the q1 and q2 values.
576 // Two subsequent q[] values are used for q1 and q2
577 // in order to obtain identical random numbers in the vector
578 // as when generating n single ones.
580 Float_t* q=new Float_t[m];
583 // Fill the vector with gaussian random numbers
584 Float_t pi=acos(-1.);
586 for (Int_t jvec=0; jvec<n; jvec++)
588 q1=q[jvec*2]; // use two subsequent q[] values
590 vec[jvec]=mean+cos(2.*pi*q2)*sigma*sqrt(-2.*log(q1));
596 cout << " *AliRandom::Gauss* Invalid value n = " << n << endl;
599 ///////////////////////////////////////////////////////////////////////////
600 void AliRandom::Gauss(Float_t* vec,Int_t n)
602 // Generate a vector of gaussian random numbers with mean=0 and sigma=1
603 // This saves lots of (member)function calls in case many random
604 // numbers are needed at once.
606 // n = The number of random numbers to be generated
610 ///////////////////////////////////////////////////////////////////////////
611 Float_t AliRandom::Poisson(Float_t mean)
613 // Generate Poisson distributed random numbers with certain mean
617 // P(n) = exp(-mean)*mean**n/n! is the Poisson distribution function
619 // with : n = 0,1,2,3,... and mean > 0
621 // To generate the distribution, the "sum trick" is used as mentioned
622 // in "Formulae and Methods in Experimental data Evaluation Vol. 1"
624 Float_t unirnp=0.; // Initialise the random number value
626 if (mean <= 0.) // Check the mean value
628 cout << " *AliRandom::Poisson* Invalid value mean = " << mean
629 << " Should be positive." << endl;
633 if (mean > 80.) // Use gaussian dist. for high mean values to save time
635 Float_t grndm=Gauss();
636 Float_t rpois=mean+grndm*sqrt(mean);
637 Int_t npois=int(rpois);
638 if ((rpois-float(npois)) > 0.5) npois++;
641 else // Construct a Poisson random number from a uniform one
644 Float_t expxm=exp(-mean);
646 while (poitst > expxm)
648 Float_t rndm=Uniform();
653 } // end of check for using Gauss method
654 } // end of mean validity checkn
657 ///////////////////////////////////////////////////////////////////////////
658 void AliRandom::Poisson(Float_t* vec,Int_t n,Float_t mean)
660 // Generate a vector of Poisson dist. random numbers with certain mean
661 // This saves lots of (member)function calls in case many random
662 // numbers are needed at once.
664 // n = The number of random numbers to be generated
668 // P(n) = exp(-mean)*mean**n/n! is the Poisson distribution function
670 // with : n = 0,1,2,3,... and mean > 0
672 // To generate the distribution, the "sum trick" is used as mentioned
673 // in "Formulae and Methods in Experimental data Evaluation Vol. 1"
675 if (n <= 0) // Check n value
677 cout << " *AliRandom::Poisson* Invalid value n = " << n << endl;
681 if (mean <= 0.) // Check the mean value
683 cout << " *AliRandom::Poisson* Invalid value mean = " << mean
684 << " Should be positive." << endl;
688 if (mean > 80.) // Use gaussian dist. for high mean values to save time
690 Float_t* grndm=new Float_t[n];
694 for (Int_t jvec=0; jvec<n; jvec++)
696 rpois=mean+grndm[jvec]*sqrt(mean);
698 if ((rpois-float(npois)) > 0.5) npois++;
699 vec[jvec]=float(npois);
703 else // Construct Poisson random numbers from a uniform ones
705 Float_t expxm=exp(-mean);
708 for (Int_t jvec=0; jvec<n; jvec++)
712 while (poitst > expxm)
714 Float_t rndm=Uniform();
718 vec[jvec]=float(npois);
720 } // end of check for using Gauss method
721 } // end of mean validity check
722 } // end of n validity check
724 ///////////////////////////////////////////////////////////////////////////
725 void AliRandom::SetUser(Float_t a,Float_t b,Int_t n,Float_t (*f)(Float_t))
727 // Determine the area under f(x) as a function of x
728 // This is called the "area function" and serves as a basis to
729 // provide random numbers in [a,b] according to the user defined
730 // distribution f(x).
731 // The area function is normalised such that the most extreme
734 fNa=n+1; // The number of bins for the area function
735 fXa=new Float_t[fNa]; // The binned x values of the area function
736 fYa=new Float_t[fNa]; // The corresponding summed f(x) values
737 fIbins=new Int_t[fNa]; // The bin numbers of the random x candidates
741 Float_t step=fabs(a-b)/float(n);
745 for (Int_t i=0; i<fNa; i++) // Fill bins of the area function
747 x=xmin+float(i)*step;
750 if (i > 0) fYa[i]+=fYa[i-1];
751 if (fabs(fYa[i]) > extreme) extreme=fabs(fYa[i]);
753 fYamin=fYa[0]/extreme;
754 fYamax=fYa[0]/extreme;
755 for (Int_t j=0; j<fNa; j++) // Normalise the area function
757 fYa[j]=fYa[j]/extreme;
758 if (fYa[j] < fYamin) fYamin=fYa[j];
759 if (fYa[j] > fYamax) fYamax=fYa[j];
762 ///////////////////////////////////////////////////////////////////////////
763 void AliRandom::SetUser(Float_t* x,Float_t* y,Int_t n)
765 // Determine the area under y[i] as a function of x[i]
766 // This is called the "area function" and serves as a basis to
767 // provide random numbers in x according to the user provided
768 // distribution (x[i],y[i]).
769 // The area function is normalised such that the most extreme
772 fNa=n; // The number of bins for the area function
773 fXa=new Float_t[fNa]; // The binned x values of the area function
774 fYa=new Float_t[fNa]; // The corresponding summed y values
775 fIbins=new Int_t[fNa]; // The bin numbers of the random x candidates
777 // Order input data with increasing x
780 for (Int_t i=1; i<fNa; i++) // Loop over x[]
782 for (Int_t j=0; j<i; j++) // Loop over xa[]
786 for (Int_t k=i; k>=j; k--) // Create empty position
791 fXa[j]=x[i]; // Put x[] value in empty position
792 fYa[j]=y[i]; // Put y[] value in empty position
793 break; // Go for next x[] value
795 if (j == i-1) // This x[] value is the largest so far
797 fXa[i]=x[i]; // Put x[] value at the end of x[]
798 fYa[i]=y[i]; // Put y[] value at the end of y[]
800 } // End loop over fXa[]
801 } // End loop over x[]
804 for (Int_t l=0; l<fNa; l++) // Fill area function
806 if (l > 0) fYa[l]+=fYa[l-1];
807 if (fabs(fYa[l]) > extreme) extreme=fabs(fYa[l]);
809 fYamin=fYa[0]/extreme;
810 fYamax=fYa[0]/extreme;
811 for (Int_t m=0; m<fNa; m++) // Normalise the area function
813 fYa[m]=fYa[m]/extreme;
814 if (fYa[m] < fYamin) fYamin=fYa[m];
815 if (fYa[m] > fYamax) fYamax=fYa[m];
818 ///////////////////////////////////////////////////////////////////////////
819 Float_t AliRandom::User()
821 // Provide a random number according to the user defined distribution
825 // Select by a uniform random number a certain area fraction (from fYa[])
826 // of the area function.
827 // The required random number is given by the corresponding x value (fXa[])
828 // of the area function.
829 // In case of more than one x value candidate, select randomly one of them.
833 Float_t ra=Uniform(fYamin,fYamax); // Random value of the area function
834 Float_t dmin=100.*fabs(fYamax-fYamin); // Init. the min. distance encountered
837 for (Int_t i=0; i<fNa; i++) // Search for fYa[] value(s) closest to ra
839 dist=fabs(ra-fYa[i]);
840 if (dist <= dmin) // fYa[i] within smallest distance --> extra candidate
843 if (dist < dmin) ncand=1; // Smallest distance so far --> THE candidate
849 Int_t jbin=0; // The bin number to hold the required x value
850 if (ncand == 1) jbin=fIbins[0];
852 if (ncand > 1) // Multiple x value candidates --> pick one randomly
854 Float_t cand=Uniform(1.,float(ncand));
855 Int_t jcand=int(cand);
856 if ((cand-float(jcand)) > 0.5) jcand++;
857 jbin=fIbins[jcand-1];
860 if (jbin > 0) // Pick randomly a value in this x-bin
862 Float_t xlow=fXa[jbin-1];
863 if (jbin > 1) xlow=fXa[jbin-2];
864 Float_t xup=fXa[jbin-1];
865 if (jbin < fNa-1) xup=fXa[jbin];
866 unirnf=Uniform(xlow,xup);
869 if (ncand == 0) cout << " *AliRandom::User* No candidate found." << endl;
873 ///////////////////////////////////////////////////////////////////////////
874 void AliRandom::User(Float_t* vec,Int_t n)
876 // Generate a vector of random numbers according to a user defined dist.
877 // This saves lots of (member)function calls in case many random
878 // numbers are needed at once.
880 // n = The number of random numbers to be generated
884 // Select by a uniform random number a certain area fraction (from fYa[])
885 // of the area function.
886 // The required random number is given by the corresponding x value (fXa[])
887 // of the area function.
888 // In case of more than one x value candidate, select randomly one of them.
890 Float_t unirnf,ra,dmin,dist;
892 for (Int_t jvec=0; jvec<n; jvec++)
895 ra=Uniform(fYamin,fYamax); // Random value of the area function
896 dmin=100.*fabs(fYamax-fYamin); // Init. the min. distance encountered
898 for (Int_t i=0; i<fNa; i++) // Search for fYa[] value(s) closest to ra
900 dist=fabs(ra-fYa[i]);
901 if (dist <= dmin) // fYa[i] within smallest distance --> extra candidate
904 if (dist < dmin) ncand=1; // Smallest distance so far --> THE candidate
910 jbin=0; // The bin number to hold the required x value
911 if (ncand == 1) jbin=fIbins[0];
913 if (ncand > 1) // Multiple x value candidates --> pick one randomly
915 Float_t cand=Uniform(1.,float(ncand));
916 Int_t jcand=int(cand);
917 if ((cand-float(jcand)) > 0.5) jcand++;
918 jbin=fIbins[jcand-1];
921 if (jbin > 0) // Pick randomly a value in this x-bin
923 Float_t xlow=fXa[jbin-1];
924 if (jbin > 1) xlow=fXa[jbin-2];
925 Float_t xup=fXa[jbin-1];
926 if (jbin < fNa-1) xup=fXa[jbin];
927 unirnf=Uniform(xlow,xup);
930 if (ncand == 0) cout << " *AliRandom::User* No candidate found." << endl;
936 ///////////////////////////////////////////////////////////////////////////