*** empty log message ***
authorMatthew Mondor <mmondor@pulsar-zone.net>
Fri, 17 Nov 2006 23:56:12 +0000 (23:56 +0000)
committerMatthew Mondor <mmondor@pulsar-zone.net>
Fri, 17 Nov 2006 23:56:12 +0000 (23:56 +0000)
mmsoftware/js/js-sh/app/randstat/randstat.js [new file with mode: 0644]

diff --git a/mmsoftware/js/js-sh/app/randstat/randstat.js b/mmsoftware/js/js-sh/app/randstat/randstat.js
new file mode 100644 (file)
index 0000000..ba72017
--- /dev/null
@@ -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
+ */