473,320 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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*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
3 1439
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I generate a random integer from 1 to N?...
3
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I generate a random integer from 1 to N?...
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I generate a random integer from 1 to N?...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.