Created
May 8, 2015 03:15
-
-
Save tsq/eca02bcc0a3a0c9a2149 to your computer and use it in GitHub Desktop.
generate random number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. 使用内置的随机数发生方法 | |
Math.random(); // 该方法产生一个0到1之间的浮点数 | |
Math.floor(Math.random() * 10 + 1); // 1-10 | |
Math.floor(Math.random() * 24); // 0-23 | |
// 2. 基于时间,亦可以产生随机数 | |
var now = new Date(); | |
var number = now.getSeconds(); // 这将产生一个基于目前时间的0到59的整数 | |
var now = new Date(); | |
var number = now.getSeconds() % 43; // 这将产生一个基于目前时间的0到42的整数 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment