负值与正值之间的随机数 [英] Random number between negative and positive value
本文介绍了负值与正值之间的随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
在特定范围内的Javascript中生成随机数?
Possible Duplicate:
Generating random numbers in Javascript in a specific range?
如何获取例如-99到99之间(不包括0)的随机值?
How can i get a random value between, for example, from -99 to 99, excluding 0?
推荐答案
var num = Math.floor(Math.random()*99) + 1; // this will get a number between 1 and 99;
num *= Math.floor(Math.random()*2) == 1 ? 1 : -1; // this will add minus sign in 50% of cases
这篇关于负值与正值之间的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文