Random number between negative and positive value [duplicate]

隐身守侯 提交于 2019-12-20 12:28:35

问题


Possible Duplicate:
Generating random numbers in Javascript in a specific range?

How can i get a random value between, for example, from -99 to 99, excluding 0?


回答1:


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



回答2:


This returns what you want

function getNonZeroRandomNumber(){
    var random = Math.floor(Math.random()*199) - 99;
    if(random==0) return getNonZeroRandomNumber();
    return random;
}

Here's a functional fiddle

EDIT

To contribute for future readers with a little debate happened in the comments which the user @MarkDickinson made a indeed relevant contribution to my first code posted, I've decided to make another fiddle with a fast comparison between using Math.floor() and Math.round() functions to return the value the op wanted.

First Scenario: Using var random = Math.round(Math.random()*198) - 99; (My first suggestion)

function getNonZeroRandomNumberWithMathRound(){
    var random = Math.round(Math.random()*198) - 99;
    if(random==0) return getNonZeroRandomNumber();
    return random;
}

Second scenario: Using var random=Math.floor(Math.random()*199) - 99; (Mark suggestion)

function getNonZeroRandomNumberWithMathFloor(){
    var random = Math.floor(Math.random()*199) - 99;
    if(random==0) return getNonZeroRandomNumber();
    return random;
}

Methodology

Since it's a short debate I've chosen fiddle.net to do the comparison.

The test consists of running the above functions 100.000 times and then retrieving how much times the extreme numbers 99 and -99 would appear against a other number, let's say 33 and -33.

The test will then give a simple output consisting of the percentage of appearances from 99 and -99 and the percentage of appearances of 33 and -33.

It'll be used the Webkit implementation from Safari 6.0.2 to the give the output from this answer but anyone can test with your favourite browser late on fiddle.net

Result from first scenario:

  • Percentage of normal ocurrences:0.97%
  • Percentage of extreme ocurrences:0.52%
  • Percentage of extreme ocurrences relative to normal ocurrences:53.4% // Half the chances indeed

Result from second scenario:

  • Percentage of normal ocurrences:1.052%
  • Percentage of extreme ocurrences:0.974%
  • Percentage of extreme ocurrences relative to normal ocurrences:92% //Closer of a fair result with a minimal standard deviation

The result can be seen here: http://jsfiddle.net/brunovieira/LrXqh/




回答3:


Here's a generalized solution that will let you set the boundaries, and opt in/out of including the 0.

var pos = 99,
    neg = 99,
    includeZero = false,
    result;

do result = Math.ceil(Math.random() * (pos + neg)) - neg;
while (includeZero === false && result === 0);

The pos and neg values are inclusive.

This way there's no requirement that the positive and negative ranges be balanced.


Or if you're worried about the rerun due to a single excluded value, you can just make the initial range less by one, and add 1 to any result greater than or equal to 0.

var pos = 5,
    neg = 5,
    result;

result = Math.floor(Math.random() * (pos + neg)) - neg;
result = result < 0 ? result : result + 1;

That last line could be shorter if you prefer:

result += (result >= 0)


来源:https://stackoverflow.com/questions/13455042/random-number-between-negative-and-positive-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!