From 0a46cb248b3a8be1acf2f275f45ea7924d43d87f Mon Sep 17 00:00:00 2001 From: liabru Date: Sat, 10 May 2014 15:01:35 +0100 Subject: [PATCH] changed Math.random to Common.random and use a seeded RNG --- demo/js/Demo.js | 31 +++++++++++++++++-------------- demo/js/DemoMobile.js | 4 +++- src/core/Common.js | 15 ++++++++++++--- 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/demo/js/Demo.js b/demo/js/Demo.js index eed58f3..054f4eb 100644 --- a/demo/js/Demo.js +++ b/demo/js/Demo.js @@ -96,7 +96,7 @@ // round the edges of some bodies var chamfer = null; - if (sides > 2 && Math.random() > 0.7) { + if (sides > 2 && Common.random() > 0.7) { chamfer = { radius: 10 }; @@ -104,7 +104,7 @@ switch (Math.round(Common.random(0, 1))) { case 0: - if (Math.random() < 0.8) { + if (Common.random() < 0.8) { return Bodies.rectangle(x, y, Common.random(25, 50), Common.random(25, 50), { chamfer: chamfer }); } else { return Bodies.rectangle(x, y, Common.random(80, 120), Common.random(25, 30), { chamfer: chamfer }); @@ -243,7 +243,7 @@ switch (Math.round(Common.random(0, 1))) { case 0: - if (Math.random() < 0.8) { + if (Common.random() < 0.8) { return Bodies.rectangle(x, y, Common.random(20, 50), Common.random(20, 50)); } else { return Bodies.rectangle(x, y, Common.random(80, 120), Common.random(20, 30)); @@ -365,7 +365,7 @@ switch (Math.round(Common.random(0, 1))) { case 0: - if (Math.random() < 0.8) { + if (Common.random() < 0.8) { return Bodies.rectangle(x, y, Common.random(20, 50), Common.random(20, 50)); } else { return Bodies.rectangle(x, y, Common.random(80, 120), Common.random(20, 30)); @@ -523,7 +523,7 @@ switch (Math.round(Common.random(0, 1))) { case 0: - if (Math.random() < 0.8) { + if (Common.random() < 0.8) { return Bodies.rectangle(x, y, Common.random(20, 50), Common.random(20, 50)); } else { return Bodies.rectangle(x, y, Common.random(80, 120), Common.random(20, 30)); @@ -551,7 +551,7 @@ switch (Math.round(Common.random(0, 1))) { case 0: - if (Math.random() < 0.8) { + if (Common.random() < 0.8) { return Bodies.rectangle(x, y, Common.random(20, 50), Common.random(20, 50)); } else { return Bodies.rectangle(x, y, Common.random(80, 120), Common.random(20, 30)); @@ -580,7 +580,7 @@ switch (Math.round(Common.random(0, 1))) { case 0: - if (Math.random() < 0.8) { + if (Common.random() < 0.8) { return Bodies.rectangle(x, y, Common.random(20, 50), Common.random(20, 50)); } else { return Bodies.rectangle(x, y, Common.random(80, 120), Common.random(20, 30)); @@ -647,8 +647,8 @@ var forceMagnitude = 0.04 * body.mass; Body.applyForce(body, { x: 0, y: 0 }, { - x: (forceMagnitude + Math.random() * forceMagnitude) * Common.choose([1, -1]), - y: -forceMagnitude + Math.random() * -forceMagnitude + x: (forceMagnitude + Common.random() * forceMagnitude) * Common.choose([1, -1]), + y: -forceMagnitude + Common.random() * -forceMagnitude }); } } @@ -699,7 +699,7 @@ switch (Math.round(Common.random(0, 1))) { case 0: - if (Math.random() < 0.8) { + if (Common.random() < 0.8) { return Bodies.rectangle(x, y, Common.random(20, 50), Common.random(20, 50), bodyOptions); } else { return Bodies.rectangle(x, y, Common.random(80, 120), Common.random(20, 30), bodyOptions); @@ -955,8 +955,8 @@ var forceMagnitude = 0.01 * body.mass; Body.applyForce(body, { x: 0, y: 0 }, { - x: (forceMagnitude + Math.random() * forceMagnitude) * Common.choose([1, -1]), - y: -forceMagnitude + Math.random() * -forceMagnitude + x: (forceMagnitude + Common.random() * forceMagnitude) * Common.choose([1, -1]), + y: -forceMagnitude + Common.random() * -forceMagnitude }); } } @@ -1069,7 +1069,7 @@ ]); var stack = Composites.stack(20, 20, 15, 4, 0, 0, function(x, y, column, row) { - if (Math.random() > 0.35) { + if (Common.random() > 0.35) { return Bodies.rectangle(x, y, 64, 64, { render: { strokeStyle: '#ffffff', @@ -1110,7 +1110,7 @@ switch (Math.round(Common.random(0, 1))) { case 0: - if (Math.random() < 0.8) { + if (Common.random() < 0.8) { return Bodies.rectangle(x, y, Common.random(20, 50), Common.random(20, 50)); } else { return Bodies.rectangle(x, y, Common.random(80, 120), Common.random(20, 30)); @@ -1275,6 +1275,9 @@ // reset id pool Common._nextId = 0; + // reset random seed + Common._seed = 0; + // reset mouse offset and scale (only required for Demo.views) Mouse.setScale(_engine.input.mouse, { x: 1, y: 1 }); Mouse.setOffset(_engine.input.mouse, { x: 0, y: 0 }); diff --git a/demo/js/DemoMobile.js b/demo/js/DemoMobile.js index 8432878..925563c 100644 --- a/demo/js/DemoMobile.js +++ b/demo/js/DemoMobile.js @@ -66,7 +66,7 @@ switch (Math.round(Common.random(0, 1))) { case 0: - if (Math.random() < 0.8) { + if (Common.random() < 0.8) { return Bodies.rectangle(x, y, Common.random(20, 40), Common.random(20, 40), { friction: 0.01, restitution: 0.4 }); } else { return Bodies.rectangle(x, y, Common.random(80, 120), Common.random(20, 30), { friction: 0.01, restitution: 0.4 }); @@ -139,6 +139,8 @@ Demo.reset = function() { var _world = _engine.world; + + Common._seed = 2; World.clear(_world); Engine.clear(_engine); diff --git a/src/core/Common.js b/src/core/Common.js index 07d1248..8b7a29a 100644 --- a/src/core/Common.js +++ b/src/core/Common.js @@ -9,6 +9,7 @@ var Common = {}; (function() { Common._nextId = 0; + Common._seed = 0; /** * Description @@ -132,7 +133,7 @@ var Common = {}; */ Common.shuffle = function(array) { for (var i = array.length - 1; i > 0; i--) { - var j = Math.floor(Math.random() * (i + 1)); + var j = Math.floor(Common.random() * (i + 1)); var temp = array[i]; array[i] = array[j]; array[j] = temp; @@ -147,7 +148,7 @@ var Common = {}; * @return {object} A random choice object from the array */ Common.choose = function(choices) { - return choices[Math.floor(Math.random() * choices.length)]; + return choices[Math.floor(Common.random() * choices.length)]; }; /** @@ -222,7 +223,9 @@ var Common = {}; * @return {number} A random number between min and max inclusive */ Common.random = function(min, max) { - return min + Math.random() * (max - min); + min = (typeof min !== "undefined") ? min : 0; + max = (typeof max !== "undefined") ? max : 1; + return min + _seededRandom() * (max - min); }; /** @@ -278,4 +281,10 @@ var Common = {}; return Common._nextId++; }; + var _seededRandom = function() { + // https://gist.github.com/ngryman/3830489 + Common._seed = (Common._seed * 9301 + 49297) % 233280; + return Common._seed / 233280; + }; + })(); \ No newline at end of file