473,747 Members | 2,862 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*Ma th.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.javas cript FAQ is at http://jibbering.com/faq/index.html.
The FAQ workers are a group of volunteers.

Feb 28 '07 #1
16 2558
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*Ma th.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.javas cript 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.javas cript message <45************ ***********@new s.sunsite.
dk>, Wed, 28 Feb 2007 00:00:02, FAQ server <ja********@dot internet.be>
posted:
>http://msdn.microsoft.com/library/de...3D/library/en-
us/script56/html/a28c5c66-c42f-4082-9b71-9a5ee4652cd7.as p
Gives me a long-winded 404.
<http://www.elsop.com/quick/quick.cgigives, checking FAQ URL,

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
>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.demo n.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.javas cript message <45************ ***********@new s.sunsite.
dk>, Wed, 28 Feb 2007 00:00:02, FAQ server <ja********@dot internet.be>
posted:
>http://msdn.microsoft.com/library/de...3D/library/en-
us/script56/html/a28c5c66-c42f-4082-9b71-9a5ee4652cd7.as p

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,

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
>http://www.merlyn.demon.co.uk/js-randm.htm
Changed.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
Feb 28 '07 #5
In comp.lang.javas cript message <Os************ ********@telcov e.net>,
Wed, 28 Feb 2007 17:24:44, FAQEditor <cl****@ctvea.n etposted:
>Dr J R Stockton said the following on 2/28/2007 12:08 PM:
>In comp.lang.javas cript message <45************ ***********@new s.sunsite.
dk>, Wed, 28 Feb 2007 00:00:02, FAQ server <ja********@dot internet.be>
posted:
>>http://msdn.microsoft.com/library/de...3D/library/en-
us/script56/html/a28c5c66-c42f-4082-9b71-9a5ee4652cd7.as p
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
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.demo n.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.javas cript message <Os************ ********@telcov e.net>,
Wed, 28 Feb 2007 17:24:44, FAQEditor <cl****@ctvea.n etposted:
>Dr J R Stockton said the following on 2/28/2007 12:08 PM:
>>In comp.lang.javas cript message <45************ ***********@new s.sunsite.
dk>, Wed, 28 Feb 2007 00:00:02, FAQ server <ja********@dot internet.be>
posted:

http://msdn.microsoft.com/library/de...3D/library/en-
us/script56/html/a28c5c66-c42f-4082-9b71-9a5ee4652cd7.as p
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

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.javas cript 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.javas cript 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.javas cript message <nY************ ********@telcov e.net>,
Thu, 1 Mar 2007 20:35:07, FAQEditor <cl****@ctvea.n etposted:
>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.demo n.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.ntlwor ld.com/stephen.chalmer s/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.demo n.co.uk/js-index.htm#Linx>

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v6.05 MIME.
<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;
Mar 2 '07 #9
In comp.lang.javas cript message <nY************ ********@telcov e.net>,
Thu, 1 Mar 2007 20:36:46, FAQEditor <cl****@ctvea.n etposted:
>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.demo n.co.uk/TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.bancoems.co m/CompLangPascalD elphiMisc-MiniFAQ.htmclpd mFAQ;
<URL:http://www.borland.com/newsgroups/guide.htmlnews: borland.* Guidelines
Mar 2 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.