最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Random Number, Math.floor(...) vs Math.ceil(...) - Stack Overflow

programmeradmin2浏览0评论

I've seen a lot of code where random numbers are generated like

// random integers in the interval [1, 10]
Math.floor(Math.random()*10 + 1)

Anyway, I feel like I'm missing something. Why don't people use the more succint way

Math.ceil(Math.random()*10);

?

I tried to test the randomness and it seems true so far.

In fact, the subsequent code

// will generate random integers from 1 to 4
var frequencies = [ 0, 0, 0, 0, 0 ]; // not using the first place
var randomNumber;
for ( var i = 0; i < 1*1000*1000; ++i ) {
   randomNumber = Math.ceil(Math.random()*4);
   frequencies[randomNumber]++;
}

for ( var i = 1; i <= 4; ++i ) {
   console.log(i +": "+ frequencies[i]);
}

prints out

1: 250103
2: 250161
3: 250163
4: 249573

What am I missing?

Quick OT: Is there a more succint way to declare and initialize frequencies? I mean like frequencies[5] = { 0 }; from C++...

I've seen a lot of code where random numbers are generated like

// random integers in the interval [1, 10]
Math.floor(Math.random()*10 + 1)

Anyway, I feel like I'm missing something. Why don't people use the more succint way

Math.ceil(Math.random()*10);

?

I tried to test the randomness and it seems true so far.

In fact, the subsequent code

// will generate random integers from 1 to 4
var frequencies = [ 0, 0, 0, 0, 0 ]; // not using the first place
var randomNumber;
for ( var i = 0; i < 1*1000*1000; ++i ) {
   randomNumber = Math.ceil(Math.random()*4);
   frequencies[randomNumber]++;
}

for ( var i = 1; i <= 4; ++i ) {
   console.log(i +": "+ frequencies[i]);
}

prints out

1: 250103
2: 250161
3: 250163
4: 249573

What am I missing?

Quick OT: Is there a more succint way to declare and initialize frequencies? I mean like frequencies[5] = { 0 }; from C++...

Share Improve this question edited Apr 5, 2013 at 10:05 Fabrizio Calderan 123k26 gold badges170 silver badges182 bronze badges asked Apr 5, 2013 at 9:43 doplumidoplumi 3,1184 gold badges33 silver badges50 bronze badges 4
  • 4 Math.ceil(Math.random()*10); generates [0,10], though the probability of 0 is very very small... – Passerby Commented Apr 5, 2013 at 9:46
  • Also, the probability of 10 is slightly smaller than 1~9, because Math.random() never return 1. – Passerby Commented Apr 5, 2013 at 9:55
  • Concerning your OT question: Not really. JavaScript does not have array initialization. JavaScript arrays are (basically) nothing more than objects with numeric key names and a length attribute. The closest you can get is new Array(size), but that does not initialize to 0, but to undefined. – jwueller Commented Apr 5, 2013 at 9:59
  • Also interesting to find that Math.ceil is ~90% slower than Math.floor : jsperf.com/convert-to-integer – Red15 Commented Dec 5, 2013 at 10:35
Add a comment  | 

3 Answers 3

Reset to default 12

as stated in MDN reference about Math.random()

Returns a floating-point, pseudo-random number in the range [0, 1) that is, from 0 (inclusive) up to but not including 1 (exclusive), which you can then scale to your desired range.

Since Math.random can return 0, then Math.ceil(Math.random()*10) could also return 0 and that value is out of your [1..10] range.


About your second question, see Most efficient way to create a zero filled JavaScript array?

Math.floor() is preferred here because of the range of Math.random().

For instance, Math.random() * 10 gives a range of [0, 10). Using Math.floor() you will never get to the value of 10, whereas Math.ceil() may give 0.

random integers in the interval [1, 10]:

Math.floor(Math.random()*10 + 1)

random integers in the interval [0, 10]:

Math.ceil(Math.random()*10);

Just depends what you need.

发布评论

评论列表(0)

  1. 暂无评论