// ----------------------------------------------------------------------------------- // /* ROOT macro for analyzing humanly generated random digits (0-9). Below are 50 digits from 15 persons, which are supposed to be random. For more information on Random Numbers: http://en.wikipedia.org/wiki/Random_number_generator Author: Troels C. Petersen (NBI/CERN) Email: Troels.Petersen@cern.ch Date: 8th of September 2010 */ // ----------------------------------------------------------------------------------- // #include #include #include //---------------------------------------------------------------------------------- void RandomDigits() //---------------------------------------------------------------------------------- { TRandom3 r; const int Nentries = 15; const int Ndigits = 50; // Diana, Logi, Sonja, Bo F, Kristoffer, Jeppe, Dan, Bo M, Lars, Alexander, Kjartan, Martin, Song, // Christoffer, Sylvia int digits[Nentries][Ndigits] = { {2,7,5,0,5,1,6,3,8,6,7,3,4,2,0,8,9,8,4,6,2,3,1,5,8,9,0,5,3,2,1,1,6,3,5,2,7,4,3,0,9,3,4,1,2,7,5,6,3,5}, {5,9,0,3,5,6,9,4,4,7,0,3,0,5,0,8,0,7,0,9,2,7,7,9,3,0,2,5,8,7,4,6,3,2,5,8,7,1,2,9,6,3,4,9,6,5,8,7,4,1}, {1,5,2,5,3,2,0,8,3,2,1,4,4,7,3,2,0,1,2,8,9,2,1,5,4,3,7,7,8,9,0,1,2,7,4,3,7,8,0,5,4,2,7,0,1,0,5,6,7,9}, {1,3,5,2,0,6,9,7,7,1,2,1,7,8,2,9,8,6,3,1,2,8,6,9,2,6,1,3,4,1,2,8,0,0,9,4,1,5,7,2,9,1,0,8,1,8,3,5,6,3}, {4,7,3,1,2,9,8,0,4,3,5,8,6,7,1,0,3,5,7,4,7,6,4,3,1,2,0,5,3,7,6,4,8,9,0,1,4,6,7,6,6,5,4,2,7,9,8,0,5,4}, {1,7,0,2,8,9,7,4,4,1,3,5,7,0,3,8,2,9,7,7,3,0,8,2,1,9,8,6,5,0,2,1,3,9,4,7,9,4,0,3,5,4,7,6,1,1,3,9,0,1}, {5,8,2,9,6,3,4,1,3,8,5,6,3,6,8,5,6,2,4,7,6,9,8,5,6,3,4,2,1,5,6,7,9,3,6,8,4,7,3,8,7,6,9,5,4,9,7,2,1,5}, {2,7,1,2,3,9,8,4,7,6,1,5,3,6,4,7,9,8,1,6,5,4,0,8,1,6,4,3,5,9,0,2,1,3,6,4,7,8,0,5,1,7,2,6,5,9,2,0,3,9}, {3,5,7,3,2,5,3,2,6,7,2,1,2,6,8,4,2,1,7,6,2,6,8,9,3,2,3,6,7,9,1,2,5,7,9,2,3,6,8,6,6,8,1,6,9,0,1,5,2,7}, {2,4,1,4,6,1,4,0,3,5,1,5,7,3,1,4,6,6,6,4,0,3,0,4,4,4,7,6,9,3,8,1,7,8,3,6,3,0,6,0,5,4,7,1,0,3,1,2,3,4}, {5,5,5,5,6,7,8,9,0,1,2,3,1,2,5,6,7,8,0,4,5,3,2,1,5,6,8,5,4,3,2,6,7,8,3,8,0,0,0,7,5,4,2,1,2,3,4,5,6,7}, {4,7,1,9,4,6,2,8,4,6,1,7,2,7,9,3,9,4,3,2,6,9,4,8,3,1,2,7,5,1,8,1,5,7,1,9,3,5,6,1,9,3,5,2,8,4,5,2,7,4}, {0,5,1,7,8,9,4,6,2,3,1,0,7,5,4,6,6,2,2,8,1,3,7,0,1,4,4,7,8,9,9,6,3,6,1,5,4,7,5,9,4,8,2,1,3,7,7,5,1,4}, {3,7,2,7,3,7,1,1,2,6,3,0,4,3,3,8,3,2,5,2,9,8,7,2,2,1,9,2,3,8,2,9,2,2,7,9,0,1,5,9,2,3,1,4,2,1,2,6,4,6}, {7,0,2,5,1,3,0,9,2,1,7,3,8,8,1,6,0,3,5,7,1,7,4,2,6,0,3,5,1,9,2,6,3,5,0,7,9,3,2,0,8,1,5,3,9,5,2,9,0,3} }; //---------------------------------------------------------------------------------- // Make histograms (name, title, number of bins, minimum, maximum): TH1F *Hist_Freq = new TH1F("Hist_Freq", "Hist_Freq", 10, -0.5, 9.5); TH2F *Hist_Corr = new TH2F("Hist_Corr", "Hist_Corr", 10, -0.5, 9.5, 10, -0.5, 9.5); int n2 = 0; //---------------------------------------------------------------------------------- // Loop over data: //---------------------------------------------------------------------------------- for (int i = 0; i < Nentries; i++) { for (int j = 0; j < Ndigits; j++) { // Check how many same digits following each other there are: int jnext1 = j+1; if (j > 48) jnext1 -= 50; if (digits[i][j] == digits[i][jnext1]) n2++; // Make plots of the frequency of each digit, and the correlation between following digits: Hist_Freq->Fill(digits[i][j]); Hist_Corr->Fill(digits[i][j],digits[i][jnext1]); } } // Print how many repeated digits were found and how many were expected: printf(" Repeating 2 digits: %2d \n", n2); // Loop over Hist_Corr bin entries (note the "+1" for the "GetBinContent"): for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { // printf(" Bin entry %1d %1d: %3d \n", i, j, Hist_Corr->GetBinContent(i+1,j+1)); } } //---------------------------------------------------------------------------------- // Plot histograms on screen: //---------------------------------------------------------------------------------- // Setting what to be shown in statistics box: gStyle->SetOptStat("e"); gStyle->SetOptFit(1111); // Making a new window (canvas): // -------------------------------------------------------------------- TCanvas* c0 = new TCanvas("c0", "", 100, 20, 600, 400); // Setting line width and color and axis titles of histogram: Hist_Freq->SetLineWidth(2); Hist_Freq->SetLineColor(4); Hist_Freq->GetXaxis()->SetTitle("Digit"); Hist_Freq->GetYaxis()->SetTitle("Frequency"); Hist_Freq->SetMinimum(0.0); // Fitting histogram with constant: TF1* fitFreq = new TF1("fitFreq", "[0]", -0.7, 9.3); fitFreq->SetParameter(0, 60.0); fitFreq->SetLineStyle(3); // Hist_Freq->Fit("fitFreq", "e"); // Drawing histogram with errors: Hist_Freq->Draw("e"); c0->Update(); // c0->SaveAs("Digit_Frequency.png"); // Making a new window (canvas): // -------------------------------------------------------------------- TCanvas* c1 = new TCanvas("c1", "", 150, 40, 600, 600); // Setting line width and color and axis titles of histogram: Hist_Corr->SetLineWidth(2); Hist_Corr->SetLineColor(4); Hist_Corr->GetXaxis()->SetTitle("Digit"); Hist_Corr->GetYaxis()->SetTitle("Following digit"); // Drawing histogram as boxes with text inside: Hist_Corr->Draw("box text"); c1->Update(); // c1->SaveAs("Digit_Correlation.png"); //---------------------------------------------------------------------------------- // Write histograms to file: //---------------------------------------------------------------------------------- TFile *file = new TFile("results.root", "RECREATE", "Histograms"); Hist_Freq->Write(); Hist_Corr->Write(); file->Write(); file->Close(); } //---------------------------------------------------------------------------------- /* First acquaint yourself with the program, and make sure that you understand what the program does (in fact, ALWAYS DO THIS!). The general question in this exercise will be to test, if the digits that you have all picked are in fact random! Questions: ---------- 1) The first histogram generated (Hist_Freq) shows the frequency of each digit. Is each digit selected a statistically equal number of times? First see what you think youself, and then uncomment line 109, such that the routine performs a fit with a constant (thus testing that hypothesis). What is the probability of the fit (given in the statistics box) and does that support the hypothesis that each digit has been chosen a statistically equal number of times? (We'll soon get to where this probability comes from). 2) Secondly, consider the number of times that a digit is repeated. If the digits were truly random, how often should this happen in our sample? How many times does it happen? And what is the uncertainty on this number? 3) The number of times that two following digits are the same in a sample should follow what distribution? And if this is the case, what is the chance that the expected number (which you calculated in question 2) should fluctuate down to the actual number observed or lower? Advanced questions: ------------------- 1) Now consider the second histogram, and make sure that you understand what this distribution is. Does it look uniform as it should be? 2) Plot each of the 100 numbers in a new histogram (make a copy of Hist_Freq and change the limits). If the numbers came from a uniform distribution (i.e. each had the same probability), then what distribution should these 100 numbers follow? Try to fit them with this distribution, and see what the result is! Hint: You might need the following function: TF1 *func = new TF1("func", "[0] * TMath::Exp(-[1]) * TMath::Power([1],x) / TMath::Gamma(x+1.0)", 0.0, 25.0); */ //----------------------------------------------------------------------------------