If num parameter is 52, how many possible return values are there? is it 52 or 53? If I understand this correctly, Math.random uses random values from 0 to 1 inclusive. If so, then 0 is a possible return value and so is 52. This results in 53 possible return values. Is this correct? Reason I ask is that a book that I’m learning from uses this code for a deck of cards. I wonder if num should equal 51 ?
Thanks …
function getRandom(num) {
var my_num = Math.floor(Math.random * num);
return my_num;
};
This will return all integers from 0 (including 0) to
num(NOT includingnum).Math.randomreturns a number between 0 (inclusive) and 1 (exclusive). Multiplying the result by X gives you between 0 (inclusive) and X (exclusive). Adding or subtracting X shifts the range by +-X.Here’s some handy functions from MDN: