前端html——获取随机数,随机例子

  • 取0-1之间的随机数(但是取不到0和1)
    var result=Math.random();

  • 取x-y之间的随机数
    var result=Math.random()*(y-x)+x;

  • 取x-y之间的随机整数
    var result=Math.floor(Math.random()*(y+1-x)+x);

  • 四舍五入
    Math.round();

随机生成课程例子

var clas=['语文','数学','英语','体育','物理','化学','地理'];
	 // 0-6之间取随机取一个
var x=Math.floor(Math.random()*(6+1-0)+0);
document.write(clas[x]);

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_41141657/article/details/87914385