From: Matthew Mondor Date: Fri, 17 Nov 2006 23:56:12 +0000 (+0000) Subject: *** empty log message *** X-Git-Tag: pgsql-branch-merge~79 X-Git-Url: http://git.pulsar-zone.net/?a=commitdiff_plain;h=e1fd1a517aa97928fc3d35aea281fd55409b8e72;p=mmondor.git *** empty log message *** --- diff --git a/mmsoftware/js/js-sh/app/randstat/randstat.js b/mmsoftware/js/js-sh/app/randstat/randstat.js new file mode 100644 index 0000000..ba72017 --- /dev/null +++ b/mmsoftware/js/js-sh/app/randstat/randstat.js @@ -0,0 +1,73 @@ +/* + * Configuration; Modify as necessary + */ +var possibilities = 4; +var maxloops = 10000; + +/* + * Variables + */ +var results = [], found = []; +var total, loop, i, rnd, found_total; + +/* + * Returns a random number between 0 and possibilities - 1 + */ +function random_possibility() +{ + + return Math.floor(Math.random() * possibilities); +} + +/* + * To get reasonable statistics, sample maxloops times + */ +for (loop = 0; loop < maxloops; loop++) { + + /* + * Flag all possibilities as unprocessed, as well as our processed + * possibilities counter which we'll use as optimization + */ + for (i = 0; i < possibilities; i++) + found[i] = false; + found_total = 0; + + /* + * Loop indefinitely increasing total from zero + */ + for (total = 0; ; total++) { + rnd = random_possibility(); + if (!found[rnd]) { + /* + * New unprocessed possibility occured, flag as such + * and increase our processed possibilities by one. + * Then verify if our counter reached the number of + * possibilities, in which case they all were + * satisfied. We then record/mark a point in our + * sparse results array and break this innner loop. + */ + found[rnd] = true; + if (++found_total == possibilities) { + if (results[total] == undefined) + results[total] = 0; + results[total]++; + break; + } + } + } + +} + +/* + * For every existing result, print its score + */ +stdout.write("Iterat.\t= Frequency:\n"); +for (total = results.length, i = 0; i < total; i++) { + if (results[i] != undefined) + stdout.write((i + 1) + "\t= " + results[i] + "\n"); +} + +/* + * XXX TODO XXX + * - It would be nice to automatically create a graphic with the results + */