Navigator 3.0; buggy in Internet Explorer 3.0; Unix platforms only for Navigator 2.0
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.
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;
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.
file: /Techref/language/java/script/definitive/refp_239.htm, 5KB, , updated: 2019/10/14 15:00, local time: 2024/11/20 04:28,
18.222.7.151: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? <A HREF="http://massmind.org/techref/language/java/script/definitive/refp_239.htm"> [Chapter 21] Reference: Math.random()</A> |
Did you find what you needed? |
Welcome to massmind.org! |
Welcome to massmind.org! |
.