简体   繁体   English

在 javascript 中生成小于 7 的伪随机数

[英]Generating pseudoRandom number less than 7 in javascript

var x = Math.floor(Math.random() * 7);
var y = Math.floor((Math.random() * 6) + 1);

Which one is correct and why??哪一个是正确的,为什么?

var x = Math.floor(Math.random() * 6); var x = Math.floor(Math.random() * 6);

because Math.random() returns a random number between 0 (inclusive), and 1 (exclusive):因为 Math.random() 返回一个介于 0(包括)和 1(不包括)之间的随机数:

description: https://www.w3schools.com/js/js_random.asp描述: https://www.w3schools.com/js/js_random.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM