By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,384 Members | 1,794 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,384 IT Pros & Developers. It's quick & easy.

FAQ Topic - How do I generate a random integer in [1..N]?

P: n/a
-----------------------------------------------------------------------
FAQ Topic - How do I generate a random integer in [1..N]?
-----------------------------------------------------------------------

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

gives a random number in the range 0..(x-1);
`` Random(N)+1 '' for [1..N]

http://msdn.microsoft.com/library/en...smthrandom.asp

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

How to Deal and Shuffle:

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://www.jibbering.com/faq/.
The FAQ workers are a group of volunteers.

Sep 3 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
JRS: In article <44***********************@news.sunsite.dk>, dated Sun,
3 Sep 2006 23:00:02 remote, seen in news:comp.lang.javascript, FAQ
server <ja********@dotinternet.beposted :
>-----------------------------------------------------------------------
FAQ Topic - How do I generate a random integer in [1..N]?
-----------------------------------------------------------------------

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

gives a random number in the range 0..(x-1);
`` Random(N)+1 '' for [1..N]

Identifier x should be renamed N or similar.

I'd change it to

FAQ Topic - How do I generate a random integer in [0..N-1]?

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

gives a random number in the range 0..(N-1);

// `` Random(N)+1 '' for [1..N]
That should be obvious. Omit?

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.merlyn.demon.co.uk/clpb-faq.txt RAH Prins : c.l.p.b mFAQ;
<URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zipTimo Salmi's Turbo Pascal FAQ.
Sep 4 '06 #2

P: n/a
Dr John Stockton wrote on 04 sep 2006 in comp.lang.javascript:
FAQ Topic - How do I generate a random integer in [0..N-1]?

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

gives a random number in the range 0..(N-1);

// `` Random(N)+1 '' for [1..N]
That should be obvious. Omit?
Omitting that would be an omission.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Sep 5 '06 #3

P: n/a
In article <QQ**************@merlyn.demon.co.uk>, Dr John Stockton
<jr*@merlyn.demon.co.ukwrites
>JRS: In article <44***********************@news.sunsite.dk>, dated Sun,
3 Sep 2006 23:00:02 remote, seen in news:comp.lang.javascript, FAQ
server <ja********@dotinternet.beposted :
>>-----------------------------------------------------------------------
FAQ Topic - How do I generate a random integer in [1..N]?
-----------------------------------------------------------------------

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

gives a random number in the range 0..(x-1);
`` Random(N)+1 '' for [1..N]


Identifier x should be renamed N or similar.
n (lower case) would be even better.

>I'd change it to

FAQ Topic - How do I generate a random integer in [0..N-1]?
Now explain what the square brackets mean, and the Pascal ".." .

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

gives a random number in the range 0..(N-1);
"0 to (n-1) inclusive"

// `` Random(N)+1 '' for [1..N]
That should be obvious. Omit?
John
--
John Harris
Sep 5 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.