Version of random that truncates ints

This commit is contained in:
Tim Mickel 2016-06-30 19:04:15 -04:00
parent bb5acd1ef4
commit 5876681bc7

View file

@ -84,16 +84,16 @@ Scratch3OperatorsBlocks.prototype.not = function (args) {
}; };
Scratch3OperatorsBlocks.prototype.random = function (args) { Scratch3OperatorsBlocks.prototype.random = function (args) {
// As a demo, this implementation of random returns after 1 second of yield. var low = args.FROM <= args.TO ? args.FROM : args.TO;
// @todo Match Scratch 2.0 implementation with int-truncation. var high = args.FROM <= args.TO ? args.TO : args.FROM;
// See: http://bit.ly/1Qc0GzC if (low == high) return low;
var examplePromise = new Promise(function(resolve) { // If both low and high are ints, truncate the result to an int.
setTimeout(function() { var lowInt = low == parseInt(low);
var res = (Math.random() * (args.TO - args.FROM)) + args.FROM; var highInt = high == parseInt(high);
resolve(res); if (lowInt && highInt) {
}, 1000); return low + parseInt(Math.random() * ((high + 1) - low));
}); }
return examplePromise; return (Math.random() * (high - low)) + low;
}; };
module.exports = Scratch3OperatorsBlocks; module.exports = Scratch3OperatorsBlocks;