ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
quasirandom.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_math
3 /// Example of generating quasi-random numbers
4 ///
5 /// \macro_image
6 /// \macro_output
7 /// \macro_code
8 ///
9 /// \author Lorenzo Moneta
10 
11 #include "Math/QuasiRandom.h"
12 #include "Math/Random.h"
13 #include "TH2.h"
14 #include "TCanvas.h"
15 #include "TStopwatch.h"
16 
17 #include <iostream>
18 
19 using namespace ROOT::Math;
20 
21 int quasirandom(int n = 10000, int skip = 0) {
22 
23  TH2D * h0 = new TH2D("h0","Pseudo-random Sequence",200,0,1,200,0,1);
24  TH2D * h1 = new TH2D("h1","Sobol Sequence",200,0,1,200,0,1);
25  TH2D * h2 = new TH2D("h2","Niederrer Sequence",200,0,1,200,0,1);
26 
27  RandomMT r0;
28  // quasi random numbers need to be created giving the dimension of the sequence
29  // in this case we generate 2-d sequence
30 
33 
34  // generate n random points
35 
36  double x[2];
37  TStopwatch w; w.Start();
38  for (int i = 0; i < n; ++i) {
39  r0.RndmArray(2,x);
40  h0->Fill(x[0],x[1]);
41  }
42  std::cout << "Time for gRandom ";
43  w.Print();
44 
45  w.Start();
46  if( skip>0) r1.Skip(skip);
47  for (int i = 0; i < n; ++i) {
48  r1.Next(x);
49  h1->Fill(x[0],x[1]);
50  }
51  std::cout << "Time for Sobol ";
52  w.Print();
53 
54  w.Start();
55  if( skip>0) r2.Skip(skip);
56  for (int i = 0; i < n; ++i) {
57  r2.Next(x);
58  h2->Fill(x[0],x[1]);
59  }
60  std::cout << "Time for Niederreiter ";
61  w.Print();
62 
63  TCanvas * c1 = new TCanvas("c1","Random sequence",600,1200);
64  c1->Divide(1,3);
65  c1->cd(1);
66  h0->Draw("COLZ");
67  c1->cd(2);
68 
69  // check uniformity
70  h1->Draw("COLZ");
71  c1->cd(3);
72  h2->Draw("COLZ");
73  gPad->Update();
74 
75  // test number of empty bins
76 
77  int nzerobins0 = 0;
78  int nzerobins1 = 0;
79  int nzerobins2 = 0;
80  for (int i = 1; i <= h1->GetNbinsX(); ++i) {
81  for (int j = 1; j <= h1->GetNbinsY(); ++j) {
82  if (h0->GetBinContent(i,j) == 0 ) nzerobins0++;
83  if (h1->GetBinContent(i,j) == 0 ) nzerobins1++;
84  if (h2->GetBinContent(i,j) == 0 ) nzerobins2++;
85  }
86  }
87 
88  std::cout << "number of empty bins for pseudo-random = " << nzerobins0 << std::endl;
89  std::cout << "number of empty bins for " << r1.Name() << "\t= " << nzerobins1 << std::endl;
90  std::cout << "number of empty bins for " << r2.Name() << "\t= " << nzerobins2 << std::endl;
91 
92  int iret = 0;
93  if (nzerobins1 >= nzerobins0 ) iret += 1;
94  if (nzerobins2 >= nzerobins0 ) iret += 2;
95  return iret;
96 
97 }
void Print(Option_t *option="") const
Print the real and cpu time passed between the start and stop events.
Definition: TStopwatch.cxx:217
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Definition: TStopwatch.cxx:56
User class for MathMore random numbers template on the Engine type.
Definition: QuasiRandom.h:60
void RndmArray(int n, double *array)
Generate an array of random numbers between ]0,1] 0 is excluded and 1 is included Function to preserv...
Definition: Random.h:69
virtual Int_t GetNbinsX() const
Definition: TH1.h:296
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition: TH2.h:90
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
unsigned int r1[N_CITIES]
Definition: simanTSP.cxx:321
tuple w
Definition: qtexample.py:51
virtual Int_t GetNbinsY() const
Definition: TH1.h:297
#define gPad
Definition: TVirtualPad.h:288
Int_t Fill(Double_t)
Invalid Fill method.
Definition: TH2.cxx:287
static char * skip(char **buf, const char *delimiters)
Definition: civetweb.c:1014
const Int_t n
Definition: legend1.C:16
unsigned int r2[N_CITIES]
Definition: simanTSP.cxx:322
Documentation for the Random class.
Definition: Random.h:41
2-D histogram with a double per channel (see TH1 documentation)}
Definition: TH2.h:297
Stopwatch class.
Definition: TStopwatch.h:30