class AlienShip {
constructor() {
//random number between 3 and 6
this.hull = Math.floor(Math.random() * 4) + 3;
//random number between 2 and 4
this.firePower = Math.floor(Math.random() * 3) + 2;
//random number between .6 and .8
this.accuracy = Math.floor(Math.random() * 1.2) + .6;
}
}
我试图在.6和.8之间得到一个随机数,它被四舍五入到十分之一。
发布于 2020-08-05 14:41:38
你可以采取一个因素和调整,在最后除以10。
console.log(Math.floor(Math.random() * 3 + 6) / 10)
发布于 2020-08-05 14:56:34
这将包括min和max。
(Math.random() * (max - min + 1)) + min
(Math.random() *(8-6+1) +6)/10
0.6至0.9
0.67,0.83.但从未超过或0.9..。
这将使min被包括在内,max被排除在外。
(Math.random() * (max - min)) + min
(Math.random() *(8-6) +6)/10
0.67,0.79.但从来没有超过0.8..。
https://stackoverflow.com/questions/63267564
复制相似问题