Better normalization... the more we fail, the more random it will become!
authorBenjamin Smedberg <benjamin@smedbergs.us>
Fri, 12 Dec 2008 15:22:48 -0500
changeset 7 266549722e4e
parent 6 a87e98928885
child 8 177f4022f463
push id1
push userbsmedberg@mozilla.com
push date2008-12-12 21:25 +0000
Better normalization... the more we fail, the more random it will become!
jswordle.js
--- a/jswordle.js
+++ b/jswordle.js
@@ -121,21 +121,21 @@ function getOwnProperties(o)
 
 function range(start, end)
 {
   for (; start < end; ++start)
     yield start;
 }
 
 /**
- * A "random" integer between two points, weighted to the center of the range.
+ * Somewhat normal distribution of results. The larger the iter, the more linear the distribution.
  */ 
-function randomInt(min, max)
+function normalInt(min, max, iter)
 {
-  return Math.floor([Math.random() for (i in range(0, 4))].reduce(function (i, j) i + j, 0) / 4 * (max - min)) + min;
+  return Math.floor([Math.random() for (i in range(0, iter))].reduce(function (i, j) i + j, 0) / iter * (max - min)) + min;
 }
 
 function getWordList()
 {
   let wordmap = {};
 
   for (let word in getWords()) {
     if (wordmap.hasOwnProperty(word)) {
@@ -381,23 +381,23 @@ function draw()
 
   let wdata, wshadow, word, size, vertical, x, y;
   /*
    * place the first word in a position randomly
    */ 
   [word, size] = words[0];
   vertical = Math.random() < kVerticalRatio * size / 88;
   [wdata, wshadow] = drawWord(cx, word, size, vertical);
-  x = randomInt(0, kWidth - wdata.width);
-  y = randomInt(0, kHeight - wdata.height);
+  x = normalInt(0, kWidth - wdata.width, 2);
+  y = normalInt(0, kHeight - wdata.height, 2);
   merge(imgdata, wdata, x, y);
 
   yield;
   
-  for (let i = 1; i < 10; ++i) {
+  for (let i = 1; i < 200; ++i) {
     let [word, size] = words[i];
 
     vertical = Math.random() < kVerticalRatio * size / 88;
     [wdata, wshadow] = drawWord(cx, word, size, vertical);
 
     for (let r in placeWord(imgdata, wshadow)) {
       if (r) {
 	[x, y] = r;
@@ -411,21 +411,21 @@ function draw()
     yield;
   }
 
   cx.putImageData(imgdata, 0, 0);
 }
 
 function placeWord(imgdata, wdata)
 {
-  let x = randomInt(0, kWidth - wdata.width);
-  let y = randomInt(0, kHeight - wdata.height);
-  for (let t = 0; t < 20; ++t) {
-    let x2 = randomInt(0, kWidth - wdata.width);
-    let y2 = randomInt(0, kWidth - wdata.height);
+  let x = normalInt(0, kWidth - wdata.width, 1);
+  let y = normalInt(0, kHeight - wdata.height, 1);
+  for (let t = 1; t < 20; ++t) {
+    let x2 = normalInt(0, kWidth - wdata.width, t);
+    let y2 = normalInt(0, kWidth - wdata.height, t);
 
     [x, y] = slide(imgdata, wdata, x, y, x2, y2);
     if (x != null) {
       yield [x, y];
       return;
     }
 
     yield;