473,746 Members | 2,658 Online

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

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

function Random(x) { return Math.floor(x*Ma th.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.javas cript FAQ is at http://www.jibbering.com/faq/.
The FAQ workers are a group of volunteers.

Sep 3 '06 #1
3 1463
JRS: In article <44************ ***********@new s.sunsite.dk>, dated Sun,
3 Sep 2006 23:00:02 remote, seen in news:comp.lang. javascript, FAQ
server <ja********@dot internet.bepost ed :
>-----------------------------------------------------------------------
FAQ Topic - How do I generate a random integer in [1..N]?
-----------------------------------------------------------------------

function Random(x) { return Math.floor(x*Ma th.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?

--
<URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.merlyn.demo n.co.uk/clpb-faq.txt RAH Prins : c.l.p.b mFAQ;
Sep 4 '06 #2
Dr John Stockton wrote on 04 sep 2006 in comp.lang.javas cript:
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.
Sep 5 '06 #3
In article <QQ************ **@merlyn.demon .co.uk>, Dr John Stockton
<jr*@merlyn.dem on.co.ukwrites
>JRS: In article <44************ ***********@new s.sunsite.dk>, dated Sun,
3 Sep 2006 23:00:02 remote, seen in news:comp.lang. javascript, FAQ
server <ja********@dot internet.bepost ed :
>>-----------------------------------------------------------------------
FAQ Topic - How do I generate a random integer in [1..N]?
-----------------------------------------------------------------------

function Random(x) { return Math.floor(x*Ma th.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 thread has been closed and replies have been disabled. Please start a new discussion.