SgtOJ
SgtOJ

Reputation: 589

Will JavaScript random function ever return a 0 or 1?

Can JavaScript's Math.random() ever return exactly a 0 or 1?

Upvotes: 17

Views: 11728

Answers (4)

Jory Cunningham
Jory Cunningham

Reputation: 742

It will not return 1

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.

See: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Math/random

Upvotes: 3

Andrew Leach
Andrew Leach

Reputation: 12983

Yes and No, in that order.

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/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.

Upvotes: 7

The Internet
The Internet

Reputation: 8123

Yes to 0, no to 1.

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/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.

Upvotes: 4

Šime Vidas
Šime Vidas

Reputation: 186013

From the ECMAScript specification:

Returns a Number value with positive sign, greater than or equal to 0 but less than 1, chosen randomly or pseudo randomly with approximately uniform distribution over that range, using an implementation-dependent algorithm or strategy. This function takes no arguments.

Source: http://ecma-international.org/ecma-262/5.1/#sec-15.8.2.14

Upvotes: 25

Related Questions