Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > JavaScript > FAQ Topic - How...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 1 Topic 35503 of 36382
Post > Topic >>

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

by "FAQ server" <javascript@[EMAIL PROTECTED] > May 6, 2008 at 11:00 PM

-----------------------------------------------------------------------
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.
 




 1 Posts in Topic:
FAQ Topic - How do I generate a random integer from 1 to N? (200
"FAQ server" &l  2008-05-06 23:00:01 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 5:10:57 CDT 2008.