负值和正值之间的随机数[重复]

2024-01-26

可能的重复:
在Javascript中生成特定范围内的随机数? https://stackoverflow.com/questions/1527803/generating-random-numbers-in-javascript-in-a-specific-range

我怎样才能得到一个随机值,例如,从-99到99,不包括0?


var num = Math.floor(Math.random()*99) + 1; // this will get a number between 1 and 99;
num *= Math.round(Math.random()) ? 1 : -1; // this will add minus sign in 50% of cases

var ranNum = Math.ceil(Math.random() * 99) * (Math.round(Math.random()) ? 1 : -1)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

负值和正值之间的随机数[重复] 的相关文章