function selectNum(lowNum,upNum) { var num = upNum-lowNum+1; // Math.floor() 向下取整 return Math.floor(Math.random()*num+lowNum); } var result = selectNum(2,8); //2~8范围内的整数
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.For example:Given n = 13,Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. 方法一 class Solution { public:
Math.random():获取0~1随机数 Math.floor() method rounds a number DOWNWARDS to the nearest integer, and returns the result. (小于等于 x,且与 x 最接近的整数.)其实返回值就是该数的整数位:Math.floor(0.666) --> 0Math.floor(39.2783) --> 39 所以我们可以使用Math.floor(Math.random())去获取你想要的一