473,767 Members | 2,152 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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*Ma th.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.javas cript FAQ is at http://www.jibbering.com/faq/.
The FAQ workers are a group of volunteers.

Sep 3 '06 #1
3 1466
JRS: In article <44************ ***********@new s.sunsite.dk>, dated Sun,
3 Sep 2006 23:00:02 remote, seen in news:comp.lang. javascript, FAQ
server <ja********@dot internet.bepost ed :
>-----------------------------------------------------------------------
FAQ Topic - How do I generate a random integer in [1..N]?
-----------------------------------------------------------------------

function Random(x) { return Math.floor(x*Ma th.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.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.
Sep 4 '06 #2
Dr John Stockton wrote on 04 sep 2006 in comp.lang.javas cript:
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.dem on.co.ukwrites
>JRS: In article <44************ ***********@new s.sunsite.dk>, dated Sun,
3 Sep 2006 23:00:02 remote, seen in news:comp.lang. javascript, FAQ
server <ja********@dot internet.bepost ed :
>>-----------------------------------------------------------------------
FAQ Topic - How do I generate a random integer in [1..N]?
-----------------------------------------------------------------------

function Random(x) { return Math.floor(x*Ma th.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
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
1271
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
1961
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
9571
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9405
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,...
1
9960
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
9841
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
7383
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
6655
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
5424
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3930
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
3
2807
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.