{
"cells": [
{
"cell_type": "markdown",
"id": "7b950110",
"metadata": {},
"source": [
"# testrandom\n",
"Performance test of all the ROOT random generator (TRandom, TRandom1, TRandom2 and TRandom3).\n",
"\n",
"Tests the generator TRandom3 against some ref values\n",
"and creates a timing table against TRandom, TRandom1 and TRandom2.\n",
"\n",
"E.g. on an an Intel Xeon Quad-core Harpertown (E5410) 2.33 GHz running\n",
"Linux SLC4 64 bit and compiled with gcc 3.4\n",
"\n",
"```\n",
"Distribution nanoseconds/call\n",
" TRandom TRandom1 TRandom2 TRandom3\n",
"Rndm.............. 5.000 105.000 7.000 10.000\n",
"RndmArray......... 4.000 104.000 6.000 9.000\n",
"Gaus.............. 36.000 180.000 40.000 48.000\n",
"Rannor............ 118.000 220.000 120.000 124.000\n",
"Landau............ 22.000 123.000 26.000 31.000\n",
"Exponential....... 93.000 198.000 98.000 104.000\n",
"Binomial(5,0.5)... 30.000 548.000 46.000 65.000\n",
"Binomial(15,0.5).. 75.000 1615.000 125.000 178.000\n",
"Poisson(3)........ 96.000 494.000 109.000 125.000\n",
"Poisson(10)....... 138.000 1236.000 165.000 203.000\n",
"Poisson(70)....... 818.000 1195.000 835.000 844.000\n",
"Poisson(100)...... 837.000 1218.000 849.000 864.000\n",
"GausTF1........... 83.000 180.000 87.000 88.000\n",
"LandauTF1......... 80.000 180.000 83.000 86.000\n",
"GausUNURAN........ 40.000 139.000 41.000 44.000\n",
"PoissonUNURAN(10). 85.000 271.000 92.000 102.000\n",
"PoissonUNURAN(100) 62.000 256.000 69.000 78.000\n",
"```\n",
"\n",
"Note that this tutorial can be executed in interpreted or compiled mode\n",
"\n",
"```cpp\n",
" Root > .x testrandom.C\n",
" Root > .x testrandom.C++\n",
"```\n",
"\n",
"\n",
"\n",
"\n",
"**Author:** Rene Brun, 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": "f382dd32",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:53.694645Z",
"iopub.status.busy": "2026-05-19T20:27:53.694508Z",
"iopub.status.idle": "2026-05-19T20:27:53.704360Z",
"shell.execute_reply": "2026-05-19T20:27:53.703795Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_44:7:2: error: unterminated conditional directive\n",
"#if __has_include(\"TUnuran.h\") // handy check\n",
" ^\n"
]
}
],
"source": [
"%%cpp -d\n",
"#include \n",
"#include \n",
"#include \n",
"#include \n",
"#include \n",
"#include \n",
"#if __has_include(\"TUnuran.h\") // handy check"
]
},
{
"cell_type": "markdown",
"id": "6b6b7abc",
"metadata": {},
"source": [
" Definition of a helper function: "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "bbb3808e",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:53.705817Z",
"iopub.status.busy": "2026-05-19T20:27:53.705696Z",
"iopub.status.idle": "2026-05-19T20:27:53.764848Z",
"shell.execute_reply": "2026-05-19T20:27:53.764354Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_45:3:2: error: #endif without #if\n",
"#endif\n",
" ^\n"
]
}
],
"source": [
"%%cpp -d\n",
"#include \n",
"#include \n",
"#endif\n",
"#include \n",
"\n",
"\n",
"void testAll() {\n",
" int i, N = 2000000;\n",
" float cpn = 1000000000./N;\n",
" int N1 = N/10; float cpn1 = cpn*10; // for TRandom1\n",
" double x,y;\n",
" TRandom *rsave = gRandom;\n",
" TRandom *r0 = new TRandom();\n",
" TRandom *r1 = new TRandom1();\n",
" TRandom *r2 = new TRandom2();\n",
" TRandom *r3 = new TRandom3();\n",
" TRandom *r4 = new TRandomMixMax();\n",
" TRandom *r5 = new TRandomMixMax17();\n",
" TRandom *r6 = new TRandomGen>();\n",
" TRandom *r7 = new TRandomMixMax256();\n",
" TRandom *r8 = new TRandomGen>();\n",
" TRandom *r9 = new TRandomMT64();\n",
" TRandom *r10 = new TRandomRanlux48();\n",
"\n",
"\n",
" TStopwatch sw;\n",
" printf(\"Distribution nanoseconds/call\\n\");\n",
" printf(\" TRandom TRandom1 TRandom2 TRandom3 MixMax240 MixMax17 Mixmax256_0 MixMax256_2 MixMax256_4 MT_64 Ranlux48\\n\");\n",
"\n",
" sw.Start();\n",
" for (i=0;iRndm(i);\n",
" }\n",
" printf(\"Rndm.............. %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iRndm(i);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn1);\n",
" sw.Start();\n",
" for (i=0;iRndm(i);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iRndm(i);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" // new random generators\n",
"\n",
" sw.Start();\n",
" for (i=0;iRndm(i);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
"\n",
" sw.Start();\n",
" for (i=0;iRndm(i);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
"\n",
" sw.Start();\n",
" for (i=0;iRndm(i);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
"\n",
" sw.Start();\n",
" for (i=0;iRndm(i);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
"\n",
" sw.Start();\n",
" for (i=0;iRndm(i);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
"\n",
" sw.Start();\n",
" for (i=0;iRndm(i);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" \n",
" sw.Start();\n",
" for (i=0;iRndm(i);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn1);\n",
"\n",
" printf(\"\\n\\n\");\n",
"\n",
" // RNDMARRAY\n",
" \n",
" const int NR = 1000;\n",
" double rn[NR];\n",
" sw.Start();\n",
" for (i=0;iRndmArray(NR,rn);\n",
" }\n",
" printf(\"RndmArray......... %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iRndmArray(NR,rn);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn1);\n",
" sw.Start();\n",
" for (i=0;iRndmArray(NR,rn);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iRndmArray(NR,rn);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iRndmArray(NR,rn);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iRndmArray(NR,rn);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iRndmArray(NR,rn);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iRndmArray(NR,rn);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iRndmArray(NR,rn);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iRndmArray(NR,rn);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iRndmArray(NR,rn);\n",
" }\n",
" printf(\" %8.3f\\n\",sw.CpuTime()*cpn1);\n",
"\n",
" // Gaus\n",
"\n",
" sw.Start();\n",
" for (i=0;iGaus(0,1);\n",
" }\n",
" printf(\"Gaus.............. %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iGaus(0,1);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn1);\n",
" sw.Start();\n",
" for (i=0;iGaus(0,1);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iGaus(0,1);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iGaus(0,1);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iGaus(0,1);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iGaus(0,1);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iGaus(0,1);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iGaus(0,1);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iGaus(0,1);\n",
" }\n",
" printf(\" %8.3f\\n\",sw.CpuTime()*cpn);\n",
"\n",
"\n",
" // RANNOR\n",
" \n",
" sw.Start();\n",
" for (i=0;iRannor(x,y);\n",
" }\n",
" printf(\"Rannor............ %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iRannor(x,y);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn1);\n",
" sw.Start();\n",
" for (i=0;iRannor(x,y);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iRannor(x,y);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iRannor(x,y);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iRannor(x,y);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iRannor(x,y);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iRannor(x,y);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iRannor(x,y);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iRannor(x,y);\n",
" }\n",
" printf(\" %8.3f\\n\",sw.CpuTime()*cpn);\n",
" \n",
" // Landau\n",
" \n",
" sw.Start();\n",
" for (i=0;iLandau(0,1);\n",
" }\n",
" printf(\"Landau............ %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iLandau(0,1);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn1);\n",
" sw.Start();\n",
" for (i=0;iLandau(0,1);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iLandau(0,1);\n",
" }\n",
" printf(\" %8.3f\\n\",sw.CpuTime()*cpn);\n",
"\n",
" sw.Start();\n",
" for (i=0;iExp(1);\n",
" }\n",
" printf(\"Exponential....... %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iExp(1);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn1);\n",
" sw.Start();\n",
" for (i=0;iExp(1);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iExp(1);\n",
" }\n",
" printf(\" %8.3f\\n\",sw.CpuTime()*cpn);\n",
"\n",
" sw.Start();\n",
" for (i=0;iBinomial(5,0.5);\n",
" }\n",
" printf(\"Binomial(5,0.5)... %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iBinomial(5,0.5);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn1);\n",
" sw.Start();\n",
" for (i=0;iBinomial(5,0.5);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iBinomial(5,0.5);\n",
" }\n",
" printf(\" %8.3f\\n\",sw.CpuTime()*cpn);\n",
"\n",
" sw.Start();\n",
" for (i=0;iBinomial(15,0.5);\n",
" }\n",
" printf(\"Binomial(15,0.5).. %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iBinomial(15,0.5);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn1);\n",
" sw.Start();\n",
" for (i=0;iBinomial(15,0.5);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iBinomial(15,0.5);\n",
" }\n",
" printf(\" %8.3f\\n\",sw.CpuTime()*cpn);\n",
"\n",
" sw.Start();\n",
" for (i=0;iPoisson(3);\n",
" }\n",
" printf(\"Poisson(3)........ %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iPoisson(3);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn1);\n",
" sw.Start();\n",
" for (i=0;iPoisson(3);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iPoisson(3);\n",
" }\n",
" printf(\" %8.3f\\n\",sw.CpuTime()*cpn);\n",
"\n",
" sw.Start();\n",
" for (i=0;iPoisson(10);\n",
" }\n",
" printf(\"Poisson(10)....... %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iPoisson(10);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn1);\n",
" sw.Start();\n",
" for (i=0;iPoisson(10);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iPoisson(10);\n",
" }\n",
" printf(\" %8.3f\\n\",sw.CpuTime()*cpn);\n",
"\n",
" sw.Start();\n",
" for (i=0;iPoisson(70);\n",
" }\n",
" printf(\"Poisson(70)....... %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iPoisson(70);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn1);\n",
" sw.Start();\n",
" for (i=0;iPoisson(70);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iPoisson(70);\n",
" }\n",
" printf(\" %8.3f\\n\",sw.CpuTime()*cpn);\n",
"\n",
" sw.Start();\n",
" for (i=0;iPoisson(100);\n",
" }\n",
" printf(\"Poisson(100)...... %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iPoisson(100);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn1);\n",
" sw.Start();\n",
" for (i=0;iPoisson(100);\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" sw.Start();\n",
" for (i=0;iPoisson(100);\n",
" }\n",
" printf(\" %8.3f\\n\",sw.CpuTime()*cpn);\n",
"\n",
" TF1 *f1 = new TF1(\"f1\",\"gaus\",-4,4);\n",
" f1->SetParameters(1,0,1);\n",
" gRandom = r0;\n",
" sw.Start();\n",
" for (i=0;iGetRandom();\n",
" }\n",
" printf(\"GausTF1........... %8.3f\",sw.CpuTime()*cpn);\n",
" gRandom = r1;\n",
" sw.Start();\n",
" for (i=0;iGetRandom();\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" gRandom = r2;\n",
" sw.Start();\n",
" for (i=0;iGetRandom();\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" gRandom = r3;\n",
" sw.Start();\n",
" for (i=0;iGetRandom();\n",
" }\n",
" printf(\" %8.3f\\n\",sw.CpuTime()*cpn);\n",
"\n",
" TF1 *f2 = new TF1(\"f2\",\"landau\",-5,15);\n",
" f2->SetParameters(1,0,1);\n",
"\n",
" gRandom = r0;\n",
" sw.Start();\n",
" for (i=0;iGetRandom();\n",
" }\n",
" printf(\"LandauTF1......... %8.3f\",sw.CpuTime()*cpn);\n",
" gRandom = r1;\n",
" sw.Start();\n",
" for (i=0;iGetRandom();\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" gRandom = r2;\n",
" sw.Start();\n",
" for (i=0;iGetRandom();\n",
" }\n",
" printf(\" %8.3f\",sw.CpuTime()*cpn);\n",
" gRandom = r3;\n",
" sw.Start();\n",
" for (i=0;iGetRandom();\n",
" }\n",
" printf(\" %8.3f\\n\",sw.CpuTime()*cpn);\n",
"\n",
" // test using Unuran\n",
"#if __has_include(\"TUnuran.h\")\n",
" TUnuran unr0(r0);\n",
" TUnuran unr1(r1);\n",
" TUnuran unr2(r2);\n",
" TUnuran unr3(r3);\n",
"\n",
" // continuous distribution (ex. Gaus)\n",
" TUnuranContDist dist(f1);\n",
" // use arou method (is probably the fastest)\n",
" unr0.Init(dist,\"arou\");\n",
" unr1.Init(dist,\"arou\");\n",
" unr2.Init(dist,\"arou\");\n",
" unr3.Init(dist,\"arou\");\n",
"\n",
" sw.Start();\n",
" for (i=0;i 10e-8) {\n",
" printf(\"i=%d x=%.8f but should be %.8f\\n\",i,x,RefValue[i]);\n",
" rc1 += 1;\n",
" }\n",
" }\n",
"\n",
" // check whether a state can be saved and restored\n",
" TFile *file = new TFile(\"random3.root\",\"RECREATE\");\n",
" file->SetCompressionLevel(0);\n",
" r.Write(\"r\");\n",
" delete file;\n",
" file = new TFile(\"random3.root\");\n",
" TRandom3 *rs = (TRandom3*) file->Get(\"r\");\n",
" for (i=0;i<1000;i++) {\n",
" if (r.Rndm() - rs->Rndm() != 0) rc2 += 1;\n",
" }\n",
" if (rc2 != 0) printf(\"state restoration failed\\n\");\n",
"\n",
" return rc1 + rc2;\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "9c6c4083",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:27:53.779530Z",
"iopub.status.busy": "2026-05-19T20:27:53.779405Z",
"iopub.status.idle": "2026-05-19T20:27:54.100222Z",
"shell.execute_reply": "2026-05-19T20:27:54.099657Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_50:2:3: error: use of undeclared identifier 'testAll'\n",
" (testAll())\n",
" ^\n",
"Error in : Error evaluating expression (testAll())\n",
"Execution of your code was aborted.\n"
]
}
],
"source": [
"testRandom3();\n",
"testAll();"
]
}
],
"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
}