Connecting Tech Pros Worldwide Forums | Help | Site Map

FAQ Topic - How do I generate a random integer from 1 to N? (2008-09-03)

FAQ server
Guest
 
Posts: n/a
#1: Sep 3 '08
-----------------------------------------------------------------------
FAQ Topic - How do I generate a random integer from 1 to
N?
-----------------------------------------------------------------------

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

function Random(x) { return Math.floor(x*Math.random()) }

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

http://msdn2.microsoft.com/en-us/library/41336409.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


--
Postings such as this are automatically sent once a day. Their
goal is to answer repeated questions, and to offer the content to
the community for continuous evaluation/improvement. The complete
comp.lang.javascript FAQ is at http://jibbering.com/faq/index.html.
The FAQ workers are a group of volunteers. The sendings of these
daily posts are proficiently hosted by http://www.pair.com.


optimistx
Guest
 
Posts: n/a
#2: Sep 3 '08

re: FAQ Topic - How do I generate a random integer from 1 to N? (2008-09-03)


FAQ server wrote:
Quote:
-----------------------------------------------------------------------
FAQ Topic - How do I generate a random integer from 1 to
N?
-----------------------------------------------------------------------
>
Method Math.random() returns a value R such that 0 <= R < 1.0 ;
therefore
>
function Random(x) { return Math.floor(x*Math.random()) }
>
gives an evenly distributed random integer in the range from 0 to
x-1 inclusive; use ` Random(N)+1 ` for 1 to N.
>
http://msdn2.microsoft.com/en-us/library/41336409.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
Excuse me, Mr/Mrs FAQ server, is that really correct? When
I try e.g. x = 1.5, do I really get evenly distributed results?

Errare humanum est. I hope I made an error: then I learned
something.


Dr_KralNOSPAM@nyc.rr.com
Guest
 
Posts: n/a
#3: Sep 3 '08

re: FAQ Topic - How do I generate a random integer from 1 to N? (2008-09-03)


Quote:
>
>Excuse me, Mr/Mrs FAQ server, is that really correct? When
>I try e.g. x = 1.5, do I really get evenly distributed results?
The argument, x, must be a positive integer.

If you want a real number between 0 and 1.5 then use x=1.5 but don't use
the floor function.

Quote:
>Errare humanum est. I hope I made an error: then I learned
>something.
Good point.


Dr J R Stockton
Guest
 
Posts: n/a
#4: Sep 3 '08

re: FAQ Topic - How do I generate a random integer from 1 to N? (2008-09-03)


In comp.lang.javascript message <48be132f$0$23607$f4da826c@news.24online
..fi>, Wed, 3 Sep 2008 07:31:39, optimistx <optimistxPoista@poistahotmail
..composted:
Quote:
>FAQ server wrote:
Quote:
>-----------------------------------------------------------------------
>FAQ Topic - How do I generate a random integer from 1 to
>N?
>-----------------------------------------------------------------------
>>
>Method Math.random() returns a value R such that 0 <= R < 1.0 ;
>therefore
>>
>function Random(x) { return Math.floor(x*Math.random()) }
>>
>gives an evenly distributed random integer in the range from 0 to
>x-1 inclusive; use ` Random(N)+1 ` for 1 to N.
>>
>http://msdn2.microsoft.com/en-us/library/41336409.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
>
>Excuse me, Mr/Mrs FAQ server, is that really correct? When
>I try e.g. x = 1.5, do I really get evenly distributed results?
>
>Errare humanum est. I hope I made an error: then I learned
>something.
ISTR that a previous FAQ maintainer was told that the argument of Random
was intended to be integer, but did not act on that (ICBW).

He also wanted to put a lower limit of 2 on x !

"therefore" should be replaced by "therefore, with positive integer x,".

However, one can use the function with other arguments, if you find it
useful.


The Microsoft and Sun references do not explicitly say that the number
is evenly distributed (I believe its evenness in imperfect in IE).

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "" (SonOfRFC1036)
Closed Thread