如何生成随机正数或负数的数字?
答案 0 :(得分:3)
将此低位和高位数作为参数提供给此函数
function randomNumber(low:Number=0, high:Number=1):Number {
return Math.floor(Math.random() * (1+high-low)) + low;
}
答案 1 :(得分:2)
生成[0,n]范围内的随机数并减去n / 2。
答案 2 :(得分:1)
private function randomize( amount:Number ):Number
{
return ( ( Math.random() - .5 ) * 2 ) * amount;
}
答案 3 :(得分:1)
var no1:Number;
var no2:Number;
no1 = Math.random() - .5;
if (no1> 0) {
no2 = no1 + (1 - no1);
}else {
no2 = no1 - (1 + no1);
}
trace(no2); // returns either 1 or -1