Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / August 2007



Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
FAQ server - 30 Aug 2007 00:00 GMT
-----------------------------------------------------------------------
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 a random integer in the range from 0 to x-1 inclusive; use
` Random(N)+1 ` for 1 to N.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/a
28c5c66-c42f-4082-9b71-9a5ee4652cd7.asp


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 www.pair.com.
Dr J R Stockton - 30 Aug 2007 22:49 GMT
In comp.lang.javascript message <46d5fa76$0$90275$14726298@news.sunsite.
dk>, Wed, 29 Aug 2007 23:00:01, FAQ server <javascript@dotinternet.be>
posted:

>FAQ Topic - How do I generate a random integer from 1 to
                     get                       in
>N?

>function Random(x) { return Math.floor(x*Math.random()) }
>
>gives a random integer in the range from 0 to x-1 inclusive; use
gives an evenly-distributed random ...

>` Random(N)+1 ` for 1 to N.
>
>http://msdn.microsoft.com/library/default.asp?url=/library/en-
>us/script56/html/a28c5c66-c42f-4082-9b71-9a5ee4652cd7.asp

That now redirects to another page.  Why does that cite pow, which cites
acos ?!?

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

There should somewhere be as equivalent page with a left margin, and/or
newer.

>How to Deal and Shuffle, See In:
                         see in:

It's a good idea to read the newsgroup c.l.j and its FAQ.  See below.

Signature

(c) John Stockton, Surrey, UK.  ?@merlyn.demon.co.uk   Turnpike v6.05   IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.