找到你要的答案

Q:get random number between 400 and 480 in javascript [duplicate]

Q:获得随机数的400和480之间的JavaScript中的[复制]

i am confused about Math.Random() in javascript in know that this function will return a number between 0 and 1

i am trying to return a number between 400 and 500 and what i did is this :

var y = Math.floor((Math.random() * 400) + 100);

but this code may return a value of 100 or 200 what is the best practice to create a number where the minimum is n0 and maximum is n1

我很困惑,关于数学的。()在JavaScript中知道,这个函数会返回一个0和1之间的数

我试图返回400和500之间的数字,我所做的是:

var y = Math.floor((Math.random() * 400) + 100);

but this code may return a value of 100 or 200 what is the best practice to create a number where the minimum is n0 and maximum is n1

answer1: 回答1:

try this:

var y = Math.floor((Math.random() * 80) + 400);

Math.random() * 80 will yield a number between 0 and 80, then just add 400 to it

试试这个:

var y = Math.floor((Math.random() * 80) + 400);

数学。()×80将产生一个0到80之间的数字,然后再给它的400

javascript