开发者

How to randomize an integer variable using JavaScript?

开发者 https://www.devze.com 2023-01-05 21:09 出处:网络
How c开发者_运维问答an I randomize a variable in JavaScript between 0-4, so that every time the page loads the variable is different but between 0 and 4?You can use Math.random() for this. To get a (w

How c开发者_运维问答an I randomize a variable in JavaScript between 0-4, so that every time the page loads the variable is different but between 0 and 4?


You can use Math.random() for this. To get a (whole) number between 0 and 4 inclusive, use the following:

var random = Math.floor(Math.random() * 5);


// Returns a random integer between min and max  
function getRandomInt(min, max)  
{  
  return Math.floor(Math.random() * (max - min + 1)) + min;  
}

from mdc (mozilla developer center)

0

精彩评论

暂无评论...
验证码 换一张
取 消