// ----------------------------------------------------------------------------------- // /* ROOT macro for analysing the degree of randomness in digits chosen by class! Author: Troels C. Petersen (NBI) Email: petersen@nbi.dk Date: 15th of September 2011 */ // ----------------------------------------------------------------------------------- // // Include from C++: #include #include #include #include // Include from ROOT: #include #include #include #include #include #include #include // ----------------------------------------------------------------------------------- // void RandomDigitsRunsTest() { // ----------------------------------------------------------------------------------- // // Set the showing of statistics and fitting results (0 means off, 1111 means all on): gROOT->Reset(); gStyle->SetOptStat(1111); gStyle->SetOptFit(1111); // Set the graphics: gStyle->SetStatBorderSize(1); gStyle->SetCanvasColor(0); bool verbose = true; // Should the digits be printet or not? (true/false) int Nlines = 0; string line; vector digits; // Vector of digits (1714 in total) // ------------------------------------ // Read data: // ------------------------------------ ifstream dataset("RandomDigits.txt"); // Open the data file. if (dataset.is_open()) { // If opening the file succeeded, then... while (!dataset.eof()) { dataset >> line; // Loop over string to get single digits: for (int i=0; i < line.size(); i++) { int dig = line[i] - '0'; // Close your eyes, and trust that this works!!! digits.push_back(dig); } Nlines++; } } dataset.close(); // Close the data file. printf(" Data in total: Lines = %3d Digits = %4d \n", Nlines, int(digits.size())); // Write the total number of lines read. // ------------------------------------ // Analyse data: // ------------------------------------ // Array for counting frequency of each digit: int Nparity0 = 0; int Nfreq[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // Loop over digits: // ----------------- for (int i=0; i < digits.size(); i++) { // Testing for change in parity (i.e. even or odd): int parity; if (digits[i]%2 == 0) {parity=0; Nparity0++;} else parity=1; // Record the frequency of each digit: Nfreq[digits[i]]++; // Print digit: if (verbose) printf("%1d", digits[i]); } if (verbose) printf("\n"); // ------------------------------------ // Calculate and print results: // ------------------------------------ // Parity tests: printf("\n Parity tests: \n ----------------- \n"); double f_parity0 = Nparity0 / double(digits.size()); double ef_parity0 = 0.0; // Figure this one out yourself! printf(" Nparity0 = %4d Ntotal = %4d \n", Nparity0, int(digits.size())); // Looking at the frequency of each digit: printf("\n Single digit frequency: \n ----------------------------- \n"); for (int i=0; i < 10; i++) { printf(" digit %1d: Nobs = %3d Nexp = ??? \n", i, Nfreq[i]); } } // ----------------------------------------------------------------------------------- // /* First look at the random digits, and see if you see any patterns? Probably not, mostly because there are many numbers, and patterns are not that visible. So, you will have to work a bit... Questions: ---------- 1) Think about what statistical tests you could submit the sample to and write down a list. Then consider, how to actually carry out these tests, and try to implement them in the program above. Try to make the list yourself first, before reading on! 2) Are there as many even as odd numbers? And do people have a tedency to choose an even number after an odd and vice versa? How about high vs. low numbers? 3) Try to count, which digits follows which digits. That should be 100 counts in total. If the numbers were truly random, what would you expect then? Is that what you observe? 4) Are people "afraid" of putting the same digit several times in a row? Advanced questions: ------------------- 1) Apart from the "repeated digit" case, are there any "irregularities" in the 90 other numbers, i.e. do people tend to like (or dislike) some patterns of numbers? */ //----------------------------------------------------------------------------------