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

F

FAQ server

-----------------------------------------------------------------------
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/d...html/a28c5c66-c42f-4082-9b71-9a5ee4652cd7.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.
 
R

Randy Webb

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 :(
 
B

Bart Van der Donck

Randy said:
FAQ server said the following on 2/27/2007 7:00 PM:


Bart, your script is broken again :(

Yes. I'm now definitely gonna choose for ` (hex 60) as to keep it
ASCII-only.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected].
http://msdn.microsoft.com/library/default.asp?url=3D/library/en-
us/script56/html/a28c5c66-c42f-4082-9b71-9a5ee4652cd7.asp

Gives me a long-winded 404.


<http://www.elsop.com/quick/quick.cgi> gives, checking FAQ URL,

Just after "Microsoft Script Debugger Download:", end of 3.2 :
00585 Error: 404 Not Found http://msdn.microsoft.com/downloads/list/web
dev.asp http://msdn.microsoft.com/downloads/list [Redir: http://msdn.mic
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


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".



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.
 
F

FAQEditor

Dr J R Stockton said the following on 2/28/2007 12:08 PM:
In comp.lang.javascript message <[email protected].


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.cgi> gives, checking FAQ URL,

Just after "Microsoft Script Debugger Download:", end of 3.2 :
00585 Error: 404 Not Found http://msdn.microsoft.com/downloads/list/web
dev.asp http://msdn.microsoft.com/downloads/list [Redir: http://msdn.mic
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.
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
will be glad to add it.
Could be taken as implying that the page is *only* about that. Fix is
to add "See in".

Changed.
 
D

Dr J R Stockton

In comp.lang.javascript message said:
Dr J R Stockton said the following on 2/28/2007 12:08 PM:

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. <pedant> They do not say whether the result can be -0 as
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.

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"

[*]
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?
 
F

FAQEditor

Dr J R Stockton said the following on 3/1/2007 10:35 AM:
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.
<pedant> They 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?
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.
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()
[*]
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?
 
F

FAQEditor

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:


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.
 
D

Dr J R Stockton

In comp.lang.javascript message said:
Dr J R Stockton said the following on 3/1/2007 10:35 AM:

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.
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()</tt> below can
return <tt>X</tt>.
There is a
<a href="http://homepage.ntlworld.com/stephen.chalmers/op6/random1.htm">
Test
Page</a> by SC.
Precautions are needed; appending <tt>%1</tt> to <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!
 
D

Dr J R Stockton

In comp.lang.javascript message said:
FAQEditor said the following on 3/1/2007 8:35 PM:

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).
 
J

John G Harris

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
 
F

FAQEditor

Dr J R Stockton said the following on 3/2/2007 8:49 AM:
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.
 
F

FAQEditor

Dr J R Stockton said the following on 3/2/2007 8:48 AM:
In comp.lang.javascript message <[email protected]>,
Thu, 1 Mar 2007 20:35:07, FAQEditor <[email protected]> posted:


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.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>,
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 <ul> should be an <ol> ... ... ... 3 != 2

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

Dr J R Stockton

In comp.lang.javascript message <[email protected]
8924D9443D28E23ED5CD>, Fri, 2 Mar 2007 20:32:59, John G Harris
<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.
 
J

John G Harris

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
 
J

John G Harris

In comp.lang.javascript message <[email protected]
8924D9443D28E23ED5CD>, Fri, 2 Mar 2007 20:32:59, John G Harris


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
be added.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top