[TOC]
  • 生成随机数字[0,1)[min, max)[min, max)整数参考资料

生成随机数字

Math.random, returns a floating-point, pseudo-random number in the range [0, 1)

[0,1)

// Returns a random number between 0 (inclusive) and 1 (exclusive)function getRandom() {  return Math.random();}

[min, max)

// Returns a random number between min (inclusive) and max (exclusive)function getRandomArbitrary(min, max) {  return Math.random() * (max - min) + min;}

[min, max)整数

// Returns a random integer between min (included) and max (excluded)// Using Math.round() will give you a non-uniform distribution!function getRandomInt(min, max) {  return Math.floor(Math.random() * (max - min)) + min;}

参考资料

  • Math random

原文: https://leohxj.gitbooks.io/front-end-database/content/problems-in-develop-webapp/generate-random.html