Skip to content

Instantly share code, notes, and snippets.

@netgfx
Last active July 20, 2017 13:38
Show Gist options
  • Select an option

  • Save netgfx/6ffcc77982db2bcabcdd80b7e11c86d0 to your computer and use it in GitHub Desktop.

Select an option

Save netgfx/6ffcc77982db2bcabcdd80b7e11c86d0 to your computer and use it in GitHub Desktop.
RandomBooleanBalancer
// from http://sbcgamesdev.blogspot.gr/2017/01/phaser-tutorial-how-to-balance-random.html
// converted to JS
// all rights reserved to Tomáš Rychnovský
var RandomBooleanBalancer = function(balancer) {
var _rnd = new Phaser.RandomDataGenerator([0,1]);
var _lbound;
var _ubound;
var _currentBalance;
// -------------------------------------------------------------------------
// balancer - how many consecutive same values maximally
function init(balancer) {
reset(balancer);
}
// -------------------------------------------------------------------------
function reset(balancer) {
balancer -= 1;
var lbound = Math.ceil(balancer / 2);
_lbound = -lbound;
_ubound = balancer - lbound;
_currentBalance = balancer === 0 ? _rnd.integerInRange(-1, 0) : 0;
}
// -------------------------------------------------------------------------
function getBoolean() {
var result = _currentBalance + _rnd.integerInRange(_lbound, _ubound);
_currentBalance += result >= 0 ? -1 : 1;
return result >= 0;
}
init(balancer);
return {
reset: function(balancer) {
reset(balancer);
},
getBoolean: function() {
return getBoolean();
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment