Skip to content

Instantly share code, notes, and snippets.

@tsq
Created May 8, 2015 03:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsq/eca02bcc0a3a0c9a2149 to your computer and use it in GitHub Desktop.
Save tsq/eca02bcc0a3a0c9a2149 to your computer and use it in GitHub Desktop.
generate random number
// 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