如何用JavaScript生成一个随机数字?
- 内容介绍
- 文章标签
- 相关推荐
本文共计156个文字,预计阅读时间需要1分钟。
plaintext生成浮点数(0到1之间):Math.random()
生成浮点数(0到max之间):Math.random() * max
生成浮点数(min到max之间):Math.random() * (max - min) + min
生成整数(0到1之间,不包括1):no integer between 0 and 1
生成整数(0到max之间,包括0但不包括max):parseInt(Math.random() * max)
Generate Float// (0, 1) Math.random(); // (0, max) Math.random() * max // (min, max) Math.random() * (max - min) + min Generate Int
// (0, 1) no integer between 0 and 1 // (0, max) parseInt(Math.random() * max); // include 0, but not include max parseInt(Math.random() * (max + 1)); // include 0 and max // (min, max) parseInt(Math.random() * (max - min) + min); // include min, but not include max parseInt(Math.random() * (max - min + 1) + min); // include min and max
本文共计156个文字,预计阅读时间需要1分钟。
plaintext生成浮点数(0到1之间):Math.random()
生成浮点数(0到max之间):Math.random() * max
生成浮点数(min到max之间):Math.random() * (max - min) + min
生成整数(0到1之间,不包括1):no integer between 0 and 1
生成整数(0到max之间,包括0但不包括max):parseInt(Math.random() * max)
Generate Float// (0, 1) Math.random(); // (0, max) Math.random() * max // (min, max) Math.random() * (max - min) + min Generate Int
// (0, 1) no integer between 0 and 1 // (0, max) parseInt(Math.random() * max); // include 0, but not include max parseInt(Math.random() * (max + 1)); // include 0 and max // (min, max) parseInt(Math.random() * (max - min) + min); // include min, but not include max parseInt(Math.random() * (max - min + 1) + min); // include min and max

