please dont rip this site

JavaScript: The Definitive Guide

Previous Chapter 21
JavaScript Reference
Next
 

Math.random() Function

Name

Math.random() Function---return a pseudo-random number

Availability

Navigator 3.0; buggy in Internet Explorer 3.0; Unix platforms only for Navigator 2.0

Synopsis

Math.random()

Arguments

None.

Returns

A pseudo-random number between 0.0 and 1.0.

Description

Math.random() returns a psuedo-random number between 0.0 and 1.0. Note that in Navigator 2.0, this function only works for Unix platforms. In Internet Explorer 3.0, Math.random() generates pseudo-random numbers, but is not randomly seeded, so it generates the same sequence of numbers each time the browser is started.

Usage

Since Math.random() does not work on all platforms, you should not rely on it if you want your scripts to be really portable. If you need only a single pseudo-random number, you can often use a portion of the current time, such as (new Date()).getSeconds()).

If you need need a sequence of more reliably pseudo-random numbers (for a game, perhaps), you'll have to write your own pseudo-random number generator. The following code is based on a linear congruential algorithm in the book Numerical Recipes, and produces simple, non-cryptographic, pseudo-random numbers:

function random() { 
  random.seed =index.html (random.seed*random.a + random.c) % random.m; 
  return random.seed / random.m; 
}
random.m=714025; random.a=4096; random.c=150889; 
random.seed = (new Date()).getTime()%random.m;

Bugs

Internet Explorer 3.0 does not randomly seed its random-number generator, so it generates the same sequence of random numbers each time the browser starts up. This bug will be fixed in a future release.

See Also

"Math"


Previous Home Next
Math.pow() Book Index Math.round()

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell

file: /Techref/language/JAVA/script/definitive/refp_239.htm, 5KB, , updated: 2019/10/14 15:00, local time: 2024/12/4 17:52,
TOP NEW HELP FIND: 
3.149.234.50:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://sxlist.com/Techref/language/JAVA/script/definitive/refp_239.htm"> [Chapter 21] Reference: Math.random()</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

 

Welcome to sxlist.com!


Site supported by
sales, advertizing,
& kind contributors
just like you!

Please don't rip/copy
(here's why

Copies of the site on CD
are available at minimal cost.
 

Welcome to sxlist.com!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .