{
"cells": [
{
"cell_type": "markdown",
"id": "2baaf4fa",
"metadata": {},
"source": [
"# quasirandom\n",
"Example of quasi-random numbers generation.\n",
"\n",
"\n",
"\n",
"\n",
"**Author:** Lorenzo Moneta \n",
"This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, May 19, 2026 at 08:27 PM."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "610815e3",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:21.410935Z",
"iopub.status.busy": "2026-05-19T20:27:21.410828Z",
"iopub.status.idle": "2026-05-19T20:27:21.729037Z",
"shell.execute_reply": "2026-05-19T20:27:21.728384Z"
}
},
"outputs": [],
"source": [
"gSystem->Load(\"libMathMore\"); "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "315c4a52",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:21.731092Z",
"iopub.status.busy": "2026-05-19T20:27:21.730973Z",
"iopub.status.idle": "2026-05-19T20:27:21.742461Z",
"shell.execute_reply": "2026-05-19T20:27:21.741988Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"#include \"Math/QuasiRandom.h\"\n",
"#include \"Math/Random.h\"\n",
"#include \"TH2.h\"\n",
"#include \"TCanvas.h\"\n",
"#include \"TStopwatch.h\"\n",
"\n",
"#include \n",
"\n",
"using namespace ROOT::Math;"
]
},
{
"cell_type": "markdown",
"id": "bbeb0917",
"metadata": {},
"source": [
" Arguments are defined. "
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "84bdbcf2",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:21.743822Z",
"iopub.status.busy": "2026-05-19T20:27:21.743716Z",
"iopub.status.idle": "2026-05-19T20:27:21.950142Z",
"shell.execute_reply": "2026-05-19T20:27:21.949515Z"
}
},
"outputs": [],
"source": [
"int n = 10000;\n",
"int skip = 0;"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "5ca0ce84",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:21.952127Z",
"iopub.status.busy": "2026-05-19T20:27:21.952012Z",
"iopub.status.idle": "2026-05-19T20:27:22.158839Z",
"shell.execute_reply": "2026-05-19T20:27:22.158270Z"
}
},
"outputs": [],
"source": [
"TH2D * h0 = new TH2D(\"h0\",\"Pseudo-random Sequence\",200,0,1,200,0,1);\n",
"TH2D * h1 = new TH2D(\"h1\",\"Sobol Sequence\",200,0,1,200,0,1);\n",
"TH2D * h2 = new TH2D(\"h2\",\"Niederrer Sequence\",200,0,1,200,0,1);\n",
"\n",
"RandomMT r0;"
]
},
{
"cell_type": "markdown",
"id": "7c86274c",
"metadata": {},
"source": [
"quasi random numbers need to be created giving the dimension of the sequence\n",
"in this case we generate 2-d sequence"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "3d4233fb",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:22.161019Z",
"iopub.status.busy": "2026-05-19T20:27:22.160904Z",
"iopub.status.idle": "2026-05-19T20:27:22.367810Z",
"shell.execute_reply": "2026-05-19T20:27:22.367235Z"
}
},
"outputs": [],
"source": [
"QuasiRandomSobol r1(2);\n",
"QuasiRandomNiederreiter r2(2);"
]
},
{
"cell_type": "markdown",
"id": "e50f7ab6",
"metadata": {},
"source": [
"generate n random points"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "2a7ff3ab",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:22.374441Z",
"iopub.status.busy": "2026-05-19T20:27:22.374318Z",
"iopub.status.idle": "2026-05-19T20:27:22.582517Z",
"shell.execute_reply": "2026-05-19T20:27:22.581688Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Time for gRandom Real time 0:00:00, CP time 0.000\n",
"Time for Sobol Real time 0:00:00, CP time 0.000\n",
"Time for Niederreiter Real time 0:00:00, CP time 0.000\n"
]
}
],
"source": [
"double x[2];\n",
"TStopwatch w; w.Start();\n",
"for (int i = 0; i < n; ++i) {\n",
" r0.RndmArray(2,x);\n",
" h0->Fill(x[0],x[1]);\n",
"}\n",
"std::cout << \"Time for gRandom \";\n",
"w.Print();\n",
"\n",
"w.Start();\n",
"if( skip>0) r1.Skip(skip);\n",
"for (int i = 0; i < n; ++i) {\n",
" r1.Next(x);\n",
" h1->Fill(x[0],x[1]);\n",
"}\n",
"std::cout << \"Time for Sobol \";\n",
"w.Print();\n",
"\n",
"w.Start();\n",
"if( skip>0) r2.Skip(skip);\n",
"for (int i = 0; i < n; ++i) {\n",
" r2.Next(x);\n",
" h2->Fill(x[0],x[1]);\n",
"}\n",
"std::cout << \"Time for Niederreiter \";\n",
"w.Print();\n",
"\n",
"TCanvas * c1 = new TCanvas(\"c1\",\"Random sequence\",600,1200);\n",
"c1->Divide(1,3);\n",
"c1->cd(1);\n",
"h0->Draw(\"COLZ\");\n",
"c1->cd(2);"
]
},
{
"cell_type": "markdown",
"id": "5a6e2aec",
"metadata": {},
"source": [
"check uniformity"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "4ceda9ac",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:22.583953Z",
"iopub.status.busy": "2026-05-19T20:27:22.583830Z",
"iopub.status.idle": "2026-05-19T20:27:22.917549Z",
"shell.execute_reply": "2026-05-19T20:27:22.917017Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
"
\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"h1->Draw(\"COLZ\");\n",
"c1->cd(3);\n",
"h2->Draw(\"COLZ\");\n",
"gPad->Update();"
]
},
{
"cell_type": "markdown",
"id": "2cfc0b48",
"metadata": {},
"source": [
"test number of empty bins"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "c2df5330",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:22.919521Z",
"iopub.status.busy": "2026-05-19T20:27:22.919396Z",
"iopub.status.idle": "2026-05-19T20:27:23.127481Z",
"shell.execute_reply": "2026-05-19T20:27:23.126691Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"number of empty bins for pseudo-random = 31139\n",
"number of empty bins for sobol\t= 30512\n",
"number of empty bins for niederreiter-base-2\t= 30512\n"
]
}
],
"source": [
"int nzerobins0 = 0;\n",
"int nzerobins1 = 0;\n",
"int nzerobins2 = 0;\n",
"for (int i = 1; i <= h1->GetNbinsX(); ++i) {\n",
" for (int j = 1; j <= h1->GetNbinsY(); ++j) {\n",
" if (h0->GetBinContent(i,j) == 0 ) nzerobins0++;\n",
" if (h1->GetBinContent(i,j) == 0 ) nzerobins1++;\n",
" if (h2->GetBinContent(i,j) == 0 ) nzerobins2++;\n",
" }\n",
"}\n",
"\n",
"std::cout << \"number of empty bins for pseudo-random = \" << nzerobins0 << std::endl;\n",
"std::cout << \"number of empty bins for \" << r1.Name() << \"\\t= \" << nzerobins1 << std::endl;\n",
"std::cout << \"number of empty bins for \" << r2.Name() << \"\\t= \" << nzerobins2 << std::endl;\n",
"\n",
"int iret = 0;\n",
"if (nzerobins1 >= nzerobins0 ) iret += 1;\n",
"if (nzerobins2 >= nzerobins0 ) iret += 2;\n",
"return iret;"
]
},
{
"cell_type": "markdown",
"id": "5c324fd8",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "29a7f76f",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:23.128880Z",
"iopub.status.busy": "2026-05-19T20:27:23.128757Z",
"iopub.status.idle": "2026-05-19T20:27:23.340711Z",
"shell.execute_reply": "2026-05-19T20:27:23.339595Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
"
\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%jsroot on\n",
"gROOT->GetListOfCanvases()->Draw()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "ROOT C++",
"language": "c++",
"name": "root"
},
"language_info": {
"codemirror_mode": "text/x-c++src",
"file_extension": ".C",
"mimetype": " text/x-c++src",
"name": "c++"
}
},
"nbformat": 4,
"nbformat_minor": 5
}