mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-23 22:42:27 -05:00
Version of random that truncates ints
This commit is contained in:
parent
bb5acd1ef4
commit
5876681bc7
1 changed files with 10 additions and 10 deletions
|
@ -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;
|
||||||
|
|
Loading…
Reference in a new issue