| 1 | /// \ingroup newran |
| 2 | ///@{ |
| 3 | |
| 4 | /// \file tryrand.cpp |
| 5 | /// Test of newran - main program and functions. |
| 6 | |
| 7 | #define WANT_STREAM |
| 8 | #define WANT_MATH |
| 9 | #define WANT_TIME |
| 10 | |
| 11 | #include "include.h" |
| 12 | #include "newran.h" |
| 13 | #include "format.h" |
| 14 | #include "utility.h" |
| 15 | #include "tryrand.h" |
| 16 | |
| 17 | |
| 18 | int main() |
| 19 | { |
| 20 | bool copy_seed_from_disk = false; |
| 21 | |
| 22 | time_lapse tl; // measure program run time |
| 23 | |
| 24 | //MultWithCarry urng(0.46875); |
| 25 | //MotherOfAll urng(0.46875); |
| 26 | LGM_mixed urng(0.46875); |
| 27 | |
| 28 | Random::Set(urng); |
| 29 | Random::SetDirectory("c:\\seed\\"); |
| 30 | if (copy_seed_from_disk) Random::CopySeedFromDisk(true); |
| 31 | |
| 32 | Real* s1; Real* s2; Real* s3; Real* s4; |
| 33 | cout << "\nBegin test\n"; // Forces cout to allocate memory at beginning |
| 34 | cout << "Now print a real number: " << 3.14159265 << endl; |
| 35 | { s1 = new Real[8000]; delete [] s1; } |
| 36 | { s3 = new Real; delete s3;} |
| 37 | |
| 38 | |
| 39 | Try |
| 40 | { |
| 41 | |
| 42 | |
| 43 | long n = 200000; |
| 44 | long n_large = 1000000; |
| 45 | |
| 46 | test1(n); |
| 47 | test2(n); |
| 48 | test3(n_large); |
| 49 | test4(n); |
| 50 | test5(n_large); |
| 51 | test6(n); |
| 52 | |
| 53 | |
| 54 | cout << "\nEnd of tests\n"; |
| 55 | |
| 56 | } |
| 57 | Catch(BaseException) |
| 58 | { |
| 59 | cout << "\nTest program fails - exception generated\n\n"; |
| 60 | cout << BaseException::what() << "\n"; |
| 61 | } |
| 62 | CatchAll |
| 63 | { |
| 64 | cout << "\nTest program fails - exception generated\n\n"; |
| 65 | } |
| 66 | |
| 67 | |
| 68 | { s2 = new Real[8000]; delete [] s2; } |
| 69 | cout << "\n(The following memory checks are probably not valid with all\n"; |
| 70 | cout << "compilers - see documentation)\n"; |
| 71 | cout << "\nChecking for lost memory: " |
| 72 | << (unsigned long)s1 << " " << (unsigned long)s2 << " "; |
| 73 | if (s1 != s2) cout << " - error\n"; else cout << " - ok\n"; |
| 74 | { s4 = new Real; delete s4;} |
| 75 | cout << "\nChecking for lost memory: " |
| 76 | << (unsigned long)s3 << " " << (unsigned long)s4 << " "; |
| 77 | if (s3 != s4) cout << " - error\n"; else cout << " - ok\n"; |
| 78 | cout << endl; |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | //***************** print ExtReal with format *********************** |
| 85 | |
| 86 | OstreamWithFormat operator<<(OstreamWithFormat os, const ExtReal& er) |
| 87 | { |
| 88 | switch (er.Code()) |
| 89 | { |
| 90 | case Finite: os << er.Value(); break; |
| 91 | case PlusInfinity: os << "plus-inf"; break; |
| 92 | case MinusInfinity: os << "minus-inf"; break; |
| 93 | case Indefinite: os << "indef"; break; |
| 94 | case Missing: os << "missing"; break; |
| 95 | } |
| 96 | return os; |
| 97 | } |
| 98 | |
| 99 | |
| 100 | //*********************** histogram function ************************* |
| 101 | |
| 102 | void Histogram(Random* rx, int n) // draw histogram with n obsv |
| 103 | { |
| 104 | int i,j; int count[20]; |
| 105 | Real* a = new Real[n]; |
| 106 | if (!a) { cout << "\nNo memory for Histogram\n"; return; } |
| 107 | for (i = 0; i < n; i++) a[i] = rx->Next(); |
| 108 | Real amax = a[0]; Real amin = a[0]; Real mean = a[0]; Real sd = 0; |
| 109 | for (i = 1; i < n; i++) |
| 110 | { |
| 111 | if (amin > a[i]) amin = a[i]; else if (amax < a[i]) amax = a[i]; |
| 112 | mean += a[i]; |
| 113 | } |
| 114 | mean /= n; |
| 115 | for (i = 0; i < 20; i++) count[i]=0; |
| 116 | for (i = 0; i < n; i++) |
| 117 | { |
| 118 | Real rat= (amax != amin) ? (a[i] - amin)/(amax - amin) : 1.0; |
| 119 | j = (int)( 19.999 * rat ); |
| 120 | if (j >= 0 && j < 20) count[j]++; |
| 121 | Real diff = a[i] - mean; sd += diff*diff; |
| 122 | } |
| 123 | sd = sqrt(sd/(n-1)); |
| 124 | j = 0; |
| 125 | for (i = 0; i < 20; i++) { if (j < count[i]) j = count[i]; } |
| 126 | if (j > 70) { for (i = 0; i < 20; i++) count[i] = (int)((70L*count[i])/j); } |
| 127 | cout << "\n"; |
| 128 | for (i = 0; i < 20; i++) |
| 129 | { cout << "\n|"; for (j = 1; j < count[i]; j = j+1) cout << "*"; } |
| 130 | cout << "\n" << rx->Name() << "\n"; |
| 131 | Format F; F.FormatType(Format::SIG_FIGS); F.Precision(5); F.Width(10); |
| 132 | cout << "p. mean = " << F << rx->Mean(); |
| 133 | cout << ", p. var = " << F << rx->Variance() << endl; |
| 134 | cout << "s. mean = " << F << mean; |
| 135 | cout << ", s. var = " << F << sd*sd; |
| 136 | cout << ", max = " << F << amax; |
| 137 | cout << ", min = " << F << amin; |
| 138 | cout << endl; |
| 139 | delete a; |
| 140 | } |
| 141 | |
| 142 | ///@} |
| 143 |