mozilla

Revision 47452 of Math.random()

  • Revision slug: JavaScript/Reference/Global_Objects/Math/random
  • Revision title: random
  • Revision id: 47452
  • Created:
  • Creator: Marcoos
  • Is current revision? No
  • Comment

Revision Content

Summary

Returns a pseudo-random number between 0 and 1. The random number generator is seeded from the current time, as in Java.

Method of Math

Static

Implemented in: JavaScript 1.0, NES 2.0: Unix only

JavaScript 1.1, NES 2.0: all platforms

ECMA Version: ECMA-262

Syntax

Math.random()

Parameters

None.

Description

Because random is a static method of Math, you always use it as Math.random(), rather than as a method of a Math object you created.

Examples

Example: Using Math.random

//Returns a random number between 0 and 1
function getRandom() {
   return Math.random()
}
{{ wiki.languages( { "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Math/random" } ) }}

Revision Source

<p>
</p>
<h3 name="Summary"> Summary </h3>
<p>Returns a pseudo-random number between 0 and 1. The random number generator is seeded from the current time, as in Java.
</p>
<table class="fullwidth-table">
<tbody><tr>
<td class="header" colspan="2">Method of <a href="en/Core_JavaScript_1.5_Reference/Objects/Math">Math</a></td>
</tr>
<tr>
<td colspan="2">
<p><b>Static</b>
</p>
</td>
</tr>
<tr>
<td>Implemented in:</td>
<td>JavaScript 1.0, NES 2.0: Unix only
<p>JavaScript 1.1, NES 2.0: all platforms
</p>
</td>
</tr>
<tr>
<td>ECMA Version:</td>
<td>ECMA-262</td>
</tr>
</tbody></table>
<h3 name="Syntax"> Syntax </h3>
<p><code>
Math.random()
</code>
</p>
<h3 name="Parameters"> Parameters </h3>
<p>None.
</p>
<h3 name="Description"> Description </h3>
<p>Because <code>random</code> is a static method of <code>Math</code>, you always use it as <code>Math.random()</code>, rather than as a method of a <code>Math</code> object you created.
</p>
<h3 name="Examples"> Examples </h3>
<h4 name="Example:_Using_Math.random"> Example: Using <code>Math.random</code> </h4>
<pre>//Returns a random number between 0 and 1
function getRandom() {
   return Math.random()
}
</pre>
{{ wiki.languages( { "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Math/random" } ) }}
Revert to this revision