From: FAQ server on
-----------------------------------------------------------------------
FAQ Topic - How do I generate a random integer from 1 to
n?
-----------------------------------------------------------------------

` Math.random() ` returns a value ` R ` such that ` 0 <= R < 1.0 `; therefore:

// positive integer expected
function getRandomNumber(n) {
return Math.floor(n * Math.random());
}

- gives an evenly distributed random integer in the range from
` 0 ` to ` n - 1 ` inclusive; use ` getRandomNumber(n)+1 ` for ` 1 ` to ` n `.

http://msdn.microsoft.com/en-us/library/41336409%28VS.85%29.aspx

http://docs.sun.com/source/816-6408-10/math.htm

How to Deal and Shuffle, see in:

http://www.merlyn.demon.co.uk/js-randm.htm


The complete comp.lang.javascript FAQ is at
http://jibbering.com/faq/

--

The sendings of these daily posts are proficiently hosted
by http://www.pair.com.

From: Dr J R Stockton on
In comp.lang.javascript message <4c02edfb$0$279$14726298(a)news.sunsite.dk
>, Sun, 30 May 2010 23:00:03, FAQ server <javascript(a)dotinternet.be>
posted:

>FAQ Topic - How do I generate a random integer from 1 to

>How to Deal and Shuffle, see in:
see via :
>
>http://www.merlyn.demon.co.uk/js-randm.htm

That page shows that, in some browsers, the default Math.random is not
as good as one might reasonably hope for.

Math constants are read-only, but Math methods can be written.

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.