469,903 Members | 2,193 Online

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

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

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

gives a random integer in the range from 0 to x-1 inclusive; use
=AB Random(N)+1 =BB for 1 to N.

http://msdn.microsoft.com/library/de...5ee4652cd7.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://jibbering.com/faq/index.html.
The FAQ workers are a group of volunteers.

Feb 28 '07 #1
16 2278
FAQ server said the following on 2/27/2007 7:00 PM:
-----------------------------------------------------------------------
FAQ Topic - How do I generate a random integer from 1 to
N?
-----------------------------------------------------------------------

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

gives a random integer in the range from 0 to x-1 inclusive; use
=AB Random(N)+1 =BB for 1 to N.
Bart, your script is broken again :(

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 28 '07 #2
Randy Webb wrote:
FAQ server said the following on 2/27/2007 7:00 PM:
>=AB Random(N)+1 =BB for 1 to N.

Bart, your script is broken again :(
Yes. I'm now definitely gonna choose for ` (hex 60) as to keep it
ASCII-only.

--
Bart

Feb 28 '07 #3
In comp.lang.javascript message <45***********************@news.sunsite.
dk>, Wed, 28 Feb 2007 00:00:02, FAQ server <ja********@dotinternet.be>
posted:
>http://msdn.microsoft.com/library/de...3D/library/en-
us/script56/html/a28c5c66-c42f-4082-9b71-9a5ee4652cd7.asp
Gives me a long-winded 404.
<http://www.elsop.com/quick/quick.cgigives, checking FAQ URL,

Just after "Microsoft Script Debugger Download:", end of 3.2 :
rosoft.com/404/default.aspx]

In 4.38 :
01478 Warning: 908 Missing / http://www.ajaxtoolbox.com
In 4.44 :
01626 Warning: 908 Missing / http://www.ajaxtoolbox.com

but the last two do not appear on my local copy of FAQ - 9.5 -
2007-01-23

>http://docs.sun.com/source/816-6408-10/math.htm
Gives false information. Says "between 0 and 1". Also says "seeded
from the current time" which is probably generally true but is not a
language requirement. I suggest a reference to the standard, 15.8.2.14,
or just copying those three lines.

>How to Deal and Shuffle:
Could be taken as implying that the page is *only* about that. Fix is
to add "See in".
>http://www.merlyn.demon.co.uk/js-randm.htm

By the way, would anyone with access to Vista care to check a Web page
in IE? It's just a matter of reporting what certain computed text says.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQqish topics, acronyms & links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Feb 28 '07 #4
Dr J R Stockton said the following on 2/28/2007 12:08 PM:
In comp.lang.javascript message <45***********************@news.sunsite.
dk>, Wed, 28 Feb 2007 00:00:02, FAQ server <ja********@dotinternet.be>
posted:
>http://msdn.microsoft.com/library/de...3D/library/en-
us/script56/html/a28c5c66-c42f-4082-9b71-9a5ee4652cd7.asp

Gives me a long-winded 404.
I just checked it and it loaded almost instantly. That is with Broadband
though but it doesn't give me a 404. Could have been a MSDN server issue
or a connection issue.
<http://www.elsop.com/quick/quick.cgigives, checking FAQ URL,

Just after "Microsoft Script Debugger Download:", end of 3.2 :
rosoft.com/404/default.aspx]
Removed as it gives me the same results.
In 4.38 :
01478 Warning: 908 Missing / http://www.ajaxtoolbox.com
In 4.44 :
01626 Warning: 908 Missing / http://www.ajaxtoolbox.com
Corrected.
but the last two do not appear on my local copy of FAQ - 9.5 -
2007-01-23
The current version is 9.55 dated 2007-02-24. Before posting this post
the version will become 9.60 dated 2007-02-28.
>http://docs.sun.com/source/816-6408-10/math.htm

Gives false information. Says "between 0 and 1".
Is that the only false information in the entire page? If it is, then I
don't see where it is that fatal.
Also says "seeded from the current time" which is probably generally true
but is not a language requirement. I suggest a reference to the standard,
15.8.2.14, or just copying those three lines.
Give me a URL to the standard and section (not just the section #) and I
>
>How to Deal and Shuffle:

Could be taken as implying that the page is *only* about that. Fix is
to add "See in".
>http://www.merlyn.demon.co.uk/js-randm.htm
Changed.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Feb 28 '07 #5
In comp.lang.javascript message <Os********************@telcove.net>,
Wed, 28 Feb 2007 17:24:44, FAQEditor <cl****@ctvea.netposted:
>Dr J R Stockton said the following on 2/28/2007 12:08 PM:
>In comp.lang.javascript message <45***********************@news.sunsite.
dk>, Wed, 28 Feb 2007 00:00:02, FAQ server <ja********@dotinternet.be>
posted:
>>http://msdn.microsoft.com/library/de...3D/library/en-
us/script56/html/a28c5c66-c42f-4082-9b71-9a5ee4652cd7.asp
Gives me a long-winded 404.

I just checked it and it loaded almost instantly. That is with
Broadband though but it doesn't give me a 404. Could have been a MSDN
server issue or a connection issue.
URL in FAQ 9.5 still gives me a *long-winded* 404. Fx - upload FAQ. It
is OK in 9.60. <pedantThey do not say whether the result can be -0 as
opposed to +0, and do not define "first loaded" </pedant>.

>but the last two do not appear on my local copy of FAQ - 9.5 -
2007-01-23

The current version is 9.55 dated 2007-02-24. Before posting this post
the version will become 9.60 dated 2007-02-28.
When a newsgroup FAQ is updated, that should be clearly announced in the
newsgroup. If Bart can pick up that information into his daily posts,
that would be ideal; maintaining it in your sig(s) would also be
effective.

>>http://docs.sun.com/source/816-6408-10/math.htm
Gives false information. Says "between 0 and 1".

Is that the only false information in the entire page? If it is, then I
don't see where it is that fatal.
Well, since we're under <see Subject>, naturally the bit about
Math.random. It is just plain WRONG.
Put at the top of 4.22 - "Method Math.random() returns a value R such
that 0 <= R < 1.0 ; therefore"
[*]
>Also says "seeded from the current time" which is probably generally
true but is not a language requirement. I suggest a reference to the
standard,
15.8.2.14, or just copying those three lines.

Give me a URL to the standard and section (not just the section #) and
I will be glad to add it.
You should know the URL of the standards; that for ECMA is (more or
less) in FAQ 2.6. As FAQ maintainer, you need to have a copy. I don't
know whether one can link to the middle of the PDF document; but a text
search for "random" or "15.8.2.14" will easily find the section.
==

WARNING : For ECMA 262, there is an Errata document. That appears NOT
to mention the error in section numbering. The ISO/IEC standard omits
that error. Therefore, section number references are not 100% portable
between the documents. But 15.8.2.14 is not affected.

NOTE : Comparing parts of the Errata for ECMA with the ISO standard, one
can see that EITHER the ISO one needs its own, non-equivalent, errata
(but not XOR) the ECMA Errata need an Errata.
===
[*] From the spec, Math.random() can return the largest possible IEEE
Double that is smaller than 1.0. Can it be shown that multiplying that
number by a positive integer N NEVER gives N as a result (assuming
IEEE-754 arithmetic) and that the result of the multiplication will
ALWAYS be rounded to N-1? Unless that can be shown (a) the function in
the FAQ is on unsound ground; (b) sound ground may not exist, for any
reasonably simple function.

That almost-1 number must be Hex 3FEF FFFF FFFF FFFF - agree, VK?

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQqish topics, acronyms & links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Mar 1 '07 #6
Dr J R Stockton said the following on 3/1/2007 10:35 AM:
In comp.lang.javascript message <Os********************@telcove.net>,
Wed, 28 Feb 2007 17:24:44, FAQEditor <cl****@ctvea.netposted:
>Dr J R Stockton said the following on 2/28/2007 12:08 PM:
>>In comp.lang.javascript message <45***********************@news.sunsite.
dk>, Wed, 28 Feb 2007 00:00:02, FAQ server <ja********@dotinternet.be>
posted:

http://msdn.microsoft.com/library/de...3D/library/en-
us/script56/html/a28c5c66-c42f-4082-9b71-9a5ee4652cd7.asp
Gives me a long-winded 404.
I just checked it and it loaded almost instantly. That is with
Broadband though but it doesn't give me a 404. Could have been a MSDN
server issue or a connection issue.

URL in FAQ 9.5 still gives me a *long-winded* 404. Fx - upload FAQ. It
is OK in 9.60.
Not sure why then as the URL itself didn't change.
<pedantThey do not say whether the result can be -0 as
opposed to +0, and do not define "first loaded" </pedant>.
Any suggested re-wording or additions to the FAQ to cover it?
>>but the last two do not appear on my local copy of FAQ - 9.5 -
2007-01-23
The current version is 9.55 dated 2007-02-24. Before posting this post
the version will become 9.60 dated 2007-02-28.

When a newsgroup FAQ is updated, that should be clearly announced in the
newsgroup.
I started posting an announcement of when I do major edits/uploads.
Maybe I can start, in the future, of keeping track of what gets changed
and post a summary.
If Bart can pick up that information into his daily posts,
that would be ideal; maintaining it in your sig(s) would also be
effective.
I can do that also for this posting name.
>>>http://docs.sun.com/source/816-6408-10/math.htm
Gives false information. Says "between 0 and 1".
Is that the only false information in the entire page? If it is, then I
don't see where it is that fatal.

Well, since we're under <see Subject>, naturally the bit about
Math.random. It is just plain WRONG.
Put at the top of 4.22 - "Method Math.random() returns a value R such
that 0 <= R < 1.0 ; therefore"
Added verbatim to the beginning of 4.22, but my own pedant is that is
not always true. Opera 7 (IIRC) was the one that could possibly return 1
from Math.random()
[*]
>>Also says "seeded from the current time" which is probably generally
true but is not a language requirement. I suggest a reference to the
standard,
15.8.2.14, or just copying those three lines.
Give me a URL to the standard and section (not just the section #) and
I will be glad to add it.

You should know the URL of the standards; that for ECMA is (more or
less) in FAQ 2.6. As FAQ maintainer, you need to have a copy.
I do have one, not as Maintainer though. But I do have a copy. Somewhere
on this PC and somewhere there is a hard-copy.

Is there a shorter URL to the specs than the sig line? And/or any other
useful URL's that could be added to this signature?

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
ECMAScript Language Specification:
http://www.ecma-international.org/pu...s/Ecma-262.htm
Mar 2 '07 #7
FAQEditor said the following on 3/1/2007 8:35 PM:
Dr J R Stockton said the following on 3/1/2007 10:35 AM:
<snip>
>Put at the top of 4.22 - "Method Math.random() returns a value R such
that 0 <= R < 1.0 ; therefore"

Added verbatim to the beginning of 4.22,
That will get uploaded after some more changes. Don't want to re-upload
and change the version number for just that.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
ECMAScript Language Specification:
http://www.ecma-international.org/pu...s/Ecma-262.htm
Mar 2 '07 #8
In comp.lang.javascript message <nY********************@telcove.net>,
Thu, 1 Mar 2007 20:35:07, FAQEditor <cl****@ctvea.netposted:
>Dr J R Stockton said the following on 3/1/2007 10:35 AM:
><pedantThey do not say whether the result can be -0 as
opposed to +0, and do not define "first loaded" </pedant>.

Any suggested re-wording or additions to the FAQ to cover it?
Not needed : it merely illustrated that even authoritative references are
not necessarily perfect.

The previously-suggested wording from Standard 15.8.2.14 includes
"positive sign" and, by saying nothing about initialisation, cannot be
imperfect in what it says about it.
>>>>http://docs.sun.com/source/816-6408-10/math.htm
Gives false information. Says "between 0 and 1".
Is that the only false information in the entire page? If it is, then I
don't see where it is that fatal.
Well, since we're under <see Subject>, naturally the bit about
Math.random. It is just plain WRONG.
Put at the top of 4.22 - "Method Math.random() returns a value R
such
that 0 <= R < 1.0 ; therefore"

Added verbatim to the beginning of 4.22, but my own pedant is that is
not always true. Opera 7 (IIRC) was the one that could possibly return 1
from Math.random()
From <URL:http://www.merlyn.demon.co.uk/js-randm.htm:

<p><em>Opera (5.02..6.01 at least), I have read, can give a value of 1.0
from its <tt>Math.random()</tt>, with a frequency of the order of one
in 35000 times - so that the function <tt>Random()</ttbelow can
return <tt>X</tt>.
There is a
<a href="http://homepage.ntlworld.com/stephen.chalmers/op6/random1.htm">
Test
Page</aby SC.
Precautions are needed; appending <tt>%1</ttto <tt>Math.random()</tt>
should do. LRN 20030804 : Opera 4, 5, not 6.05.</em></p>
>Is there a shorter URL to the specs than the sig line?
You could make one, near enough, by writing "via FAQ 2.6" which section
ought also to cite both the Errata to ECMA-262 and ISO 16262.
And/or any other useful URL's that could be added to this signature?
http://www.jibbering.com/faq/faq_notes/faq_notes.html - although that is
both anonymous and undated.

For more, look in <URL:http://www.merlyn.demon.co.uk/js-index.htm#Linx>
but please don't link directly to that URL from your sig!

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 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.
Mar 2 '07 #9
In comp.lang.javascript message <nY********************@telcove.net>,
Thu, 1 Mar 2007 20:36:46, FAQEditor <cl****@ctvea.netposted:
>FAQEditor said the following on 3/1/2007 8:35 PM:
>Dr J R Stockton said the following on 3/1/2007 10:35 AM:
>>Put at the top of 4.22 - "Method Math.random() returns a value R such
that 0 <= R < 1.0 ; therefore"
Added verbatim to the beginning of 4.22,

That will get uploaded after some more changes. Don't want to re-upload
and change the version number for just that.

Indeed not. But, when a section is significantly changed, you might
post here a simple copy'n'paste from the browser view of the new version
(which I presume you see locally).

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Delphi 3? Turnpike 6.05
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.bancoems.com/CompLangPascalDelphiMisc-MiniFAQ.htmclpdmFAQ;
<URL:http://www.borland.com/newsgroups/guide.htmlnews:borland.* Guidelines
Mar 2 '07 #10
On Thu, 1 Mar 2007 at 15:35:30, in comp.lang.javascript, Dr J R Stockton
wrote:

<snip>
>WARNING : For ECMA 262, there is an Errata document.
<snip>

You quoted a URL recently. It's not an official ECMA document. What is
the status of the author? Can he be trusted well enough to be quoted in
the FAQ?

John
--
John Harris
Mar 2 '07 #11
Dr J R Stockton said the following on 3/2/2007 8:49 AM:
In comp.lang.javascript message <nY********************@telcove.net>,
Thu, 1 Mar 2007 20:36:46, FAQEditor <cl****@ctvea.netposted:
>FAQEditor said the following on 3/1/2007 8:35 PM:
>>Dr J R Stockton said the following on 3/1/2007 10:35 AM:
>>>Put at the top of 4.22 - "Method Math.random() returns a value R such
that 0 <= R < 1.0 ; therefore"
Added verbatim to the beginning of 4.22,
That will get uploaded after some more changes. Don't want to re-upload
and change the version number for just that.

Indeed not. But, when a section is significantly changed, you might
post here a simple copy'n'paste from the browser view of the new version
(which I presume you see locally).
Method Math.random() returns a value R such that 0 <= R < 1.0 ; therefore
<code>
function Random(x) { return Math.floor(x*Math.random()) }
</code>
gives a random integer in the range from 0 to x-1 inclusive; use
Random(N)+1 for 1 to N.

Is what I see locally.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
ECMAScript Language Specification:
http://www.ecma-international.org/pu...s/Ecma-262.htm
Mar 2 '07 #12
Dr J R Stockton said the following on 3/2/2007 8:48 AM:
In comp.lang.javascript message <nY********************@telcove.net>,
Thu, 1 Mar 2007 20:35:07, FAQEditor <cl****@ctvea.netposted:
<snip>
>And/or any other useful URL's that could be added to this signature?

http://www.jibbering.com/faq/faq_notes/faq_notes.html - although that is
both anonymous and undated.

For more, look in <URL:http://www.merlyn.demon.co.uk/js-index.htm#Linx>
but please don't link directly to that URL from your sig!
I won't :-)

I removed my name to leave 4 lines of information. Thoughts? I don't
like the via FAQ4.26 but it is better than the wrapped URL.

--
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
FAQ Notes: http://www.jibbering.com/faq/faq_notes/faq_notes.html
ECMAScript Language Specification via FAQ4.26
CLJ Newbie FAQ: http://www.javascripttoolbox.com/clj/
Mar 2 '07 #13
In comp.lang.javascript message <pO********************@telcove.net>,
Fri, 2 Mar 2007 16:56:49, FAQEditor <cl****@ctvea.netposted:
>I removed my name to leave 4 lines of information. Thoughts? I don't
like the via FAQ4.26 but it is better than the wrapped URL.
I think "FAQ 2.6" would be better.
>--
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
FAQ Notes: http://www.jibbering.com/faq/faq_notes/faq_notes.html
ECMAScript Language Specification via FAQ4.26
CLJ Newbie FAQ: http://www.javascripttoolbox.com/clj/
USENET best practice requires name in sig :-

A "personal signature" is a short closing text automatically added to
the end of articles by posting agents, identifying the poster and
giving his network addresses, etc.

I don't know whether that "Newbie FAQ" URL is appropriate for a FAQ-
maintainer's sig; it fails http://validator.w3.org/ with 12 errors
(though my pages have the first of those) and also fails CSS at
http://jigsaw.w3.org/css-validator/ .

It also fails careful reading. Split infinitives may be OK in its
language, but ...

There are two points to remember when replying to posts to
ensure that you interact well with others:

* Don't top-post
* Quote what you're replying to
* Leave proper attribution lines in so others know who said what

Perhaps that <ulshould be an <ol... ... ... 3 != 2

I'd suggest "only" after "quote". Phrase "proper attribution" is
undefined. Also one should create proper attributions.

--
(c) John Stockton, Surrey, UK. REPLYyyww merlyn demon co uk Turnpike 6.05.
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html-Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demon.co.uk/news-use.htm: about usage of News.
No Encoding. Quotes precede replies. Snip well. Write clearly. Mail no News.
Mar 3 '07 #14
In comp.lang.javascript message <aQ**************@J.A830F0FF37FB96852AD0
8924D9443D28E23ED5CD>, Fri, 2 Mar 2007 20:32:59, John G Harris
<jo**@nospam.demon.co.ukposted:
>On Thu, 1 Mar 2007 at 15:35:30, in comp.lang.javascript, Dr J R Stockton
wrote:

<snip>
>>WARNING : For ECMA 262, there is an Errata document.
<snip>

You quoted a URL recently. It's not an official ECMA document. What is
the status of the author? Can he be trusted well enough to be quoted in
the FAQ?
Well, it is at http://www.mozilla.org/ and at the foot is a link to
the author, who could perhaps be asked. Google finds "The august
Waldemar Horwat -- who was at one time the lead Javascript developer at
AOL-Time-Warner-Netscape". Jim may know of him.
By the way, would anyone with access to Vista care to check a Web page
in IE? It's just a matter of reporting what certain computed text says.
--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQqish topics, acronyms & links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
Eclipse tonight, especiall well-timed for those on GMT locally
Mar 3 '07 #15
On Sat, 3 Mar 2007 at 15:15:47, in comp.lang.javascript, Dr J R Stockton
wrote:
>In comp.lang.javascript message <aQ**************@J.A830F0FF37FB96852AD0
8924D9443D28E23ED5CD>, Fri, 2 Mar 2007 20:32:59, John G Harris
<jo**@nospam.demon.co.ukposted:
>>On Thu, 1 Mar 2007 at 15:35:30, in comp.lang.javascript, Dr J R Stockton
wrote:

<snip>
>>>WARNING : For ECMA 262, there is an Errata document.
<snip>

You quoted a URL recently. It's not an official ECMA document. What is
the status of the author? Can he be trusted well enough to be quoted in
the FAQ?

Well, it is at http://www.mozilla.org/
Mozilla is only one browser manufacturer. There are others.

It does not say if it is being maintained.

It does not say if outsiders, such as you, can ask for another change to

It looks ok, but if the FAQ refers to it it should be flagged as
unofficial and possibly incomplete now.
>and at the foot is a link to
the author, who could perhaps be asked. Google finds "The august
Waldemar Horwat -- who was at one time the lead Javascript developer at
AOL-Time-Warner-Netscape". Jim may know of him.
Is he the one who built a Y2k bug into a language invented in the mid
1990s ?

John
--
John Harris
Mar 4 '07 #16
On Sat, 3 Mar 2007 at 14:30:54, in comp.lang.javascript, Dr J R Stockton
wrote:

<snip>
>Split infinitives may be OK in its
language, but ...
<snip>

According to the official style manual for the British Civil Service,
"Plain Words", splitting an infinitive is good English grammar, often
ugly, and sometimes essential. The manual follows Fowler and Partridge
who were leading experts on the language.

On being asked to tea with the great dictator :-
To go is dangerous.
To not go is fatal.

John
--
John Harris
Mar 4 '07 #17

### This discussion thread is closed

Replies have been disabled for this discussion.