473,747 Members | 2,862 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,

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

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

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.
>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
will be glad to add it.
>
>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.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
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.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
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.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>
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.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;
<URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zipTimo Salmi's Turbo Pascal FAQ.
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.

Similar topics

3
1464
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I generate a random integer in ? ----------------------------------------------------------------------- function Random(x) { return Math.floor(x*Math.random()) } gives a random number in the range 0..(x-1); `` Random(N)+1 '' for http://msdn.microsoft.com/library/en-us/script56/html/js56jsmthrandom.asp
1
338
by: FAQ server | last post by:
----------------------------------------------------------------------- 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 « Random(N)+1 » for 1 to N where N>2. ...
3
1270
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I generate a random integer from 1 to N? ----------------------------------------------------------------------- Method Math.random() returns a value R such that 0 <= R < 1.0 ; therefore function Random(x) { return Math.floor(x*Math.random()) } gives an evenly distributed random integer in the range from 0 to
1
1960
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I generate a random integer from 1 to N? ----------------------------------------------------------------------- ` Math.random() ` returns a value ` R ` such that ` 0 <= R < 1.0 `; therefore: // positive integer expected function Random(j) { return Math.floor(j * Math.random());
0
8818
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9354
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9307
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9223
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6790
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6067
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4860
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3296
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2771
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.