Skip to content

Commit

Permalink
changed Math.random to Common.random and use a seeded RNG
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed May 10, 2014
1 parent 3043baa commit 0a46cb2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
31 changes: 17 additions & 14 deletions demo/js/Demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@

// 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
};
}

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 });
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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
});
}
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
});
}
}
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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 });
Expand Down
4 changes: 3 additions & 1 deletion demo/js/DemoMobile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down Expand Up @@ -139,6 +139,8 @@

Demo.reset = function() {
var _world = _engine.world;

Common._seed = 2;

World.clear(_world);
Engine.clear(_engine);
Expand Down
15 changes: 12 additions & 3 deletions src/core/Common.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var Common = {};
(function() {

Common._nextId = 0;
Common._seed = 0;

/**
* Description
Expand Down Expand Up @@ -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;
Expand All @@ -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)];
};

/**
Expand Down Expand Up @@ -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);
};

/**
Expand Down Expand Up @@ -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;
};

})();

0 comments on commit 0a46cb2

Please sign in to comment.