Is There Any Math. Function Which Gives Sequence Of Numbers When Ever I Call It?
I have the following function: function random() { var whichProduct = Math.floor(Math.random()*5); UIALogger.logMessage(whichProduct); } random(); This will give a rando
Solution 1:
I don't think there is anything built in to the language but you can define your own like so:
var increasingSequence = function(a, b) {
var seq=[], min=Math.min(a,b), max=Math.max(a,b), i;
for (i=min; i<max; i++) {
seq.push(i);
}
return seq;
};
increasingSequence(1, 5); // => [1, 2, 3, 4, 5]
increasingSequence(2, -2); // => [-2, -1, 0, 1, 2]
Note also that Math.random()
returns a value in the range [0,1)
(that is including zero but not including one), so if you want your random function to return [1,5]
then you should implement it like so:
Math.floor(Math.random() * 5) + 1;
Post a Comment for "Is There Any Math. Function Which Gives Sequence Of Numbers When Ever I Call It?"