| 1 |
#include <tads.h> |
| 2 |
#include <bignum.h> |
| 3 |
|
| 4 |
/* |
| 5 |
* Test of random number generator distribution for percentage tests. |
| 6 |
* We'll generate a set of values in the range 0 to 99, and check to see |
| 7 |
* what percentage fall under the given percent value. |
| 8 |
*/ |
| 9 |
main(args) |
| 10 |
{ |
| 11 |
local pct; |
| 12 |
local numReport; |
| 13 |
local numTotal; |
| 14 |
local numUnder; |
| 15 |
|
| 16 |
/* check for the percentage argument */ |
| 17 |
if (args.length() != 2 |
| 18 |
|| (pct = toInteger(args[2])) < 1 || pct > 100) |
| 19 |
{ |
| 20 |
"usage: t3run randpct <pct>\n |
| 21 |
'pct' is a number from 1 to 100; we'll generate a set of |
| 22 |
random values from 0 to 99, and determine how many are less than |
| 23 |
the 'pct'. A good uniform random number generator will |
| 24 |
average out over a large number of trials so that 'pct' percent |
| 25 |
of the values are less than 'pct'.\n"; |
| 26 |
return; |
| 27 |
} |
| 28 |
|
| 29 |
/* zero the counters */ |
| 30 |
numTotal = numUnder = 0; |
| 31 |
|
| 32 |
/* seed the random number generator */ |
| 33 |
randomize(); |
| 34 |
|
| 35 |
/* run our tests, reporting at several points */ |
| 36 |
foreach (numReport in [100, 1000, 10000, 50000]) |
| 37 |
{ |
| 38 |
/* run tests until we get to the next reporting point */ |
| 39 |
for ( ; numTotal < numReport ; ++numTotal) |
| 40 |
{ |
| 41 |
/* |
| 42 |
* generate a random number in the range 0 to 99, and note if |
| 43 |
* it's less than 'pct' |
| 44 |
*/ |
| 45 |
if (rand(100) < pct) |
| 46 |
++numUnder; |
| 47 |
} |
| 48 |
|
| 49 |
/* report the distribution so far */ |
| 50 |
"Total samples = <<numTotal>>; samples < <<pct>> = <<numUnder>> |
| 51 |
(<<(100.0 * numUnder / numTotal).formatString(5, 0, 3, 2)>>%)\n"; |
| 52 |
} |
| 53 |
} |
| 54 |
|