js 生成随机数,三种方式

//生成 [1,max] 的随机数: max - 期望的最大值
			random1(max) {
				console.log(parseInt(Math.random() * max, 10) + 1);
				console.log(Math.floor(Math.random() * max) + 1);
				console.log(Math.ceil(Math.random() * max));
			},
			// 生成 [0,max] 到任意数的随机数,公式如下:
			random2(max) {
				console.log(parseInt(Math.random() * (max + 1), 10))
				console.log(Math.floor(Math.random() * (max + 1)))
			},
			//生成区间  max - 期望的最大值 min - 期望的最小值
			random3(min, max) {
				// max - 期望的最大值
				// min - 期望的最小值
				console.log(parseInt(Math.random() * (max - min + 1) + min, 10));
				console.log(Math.floor(Math.random() * (max - min + 1) + min))
			}
Posted in JS

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注