473,746 Members | 2,658 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 1463
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
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
8800
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
9350
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
9285
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
9218
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...
0
8229
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6060
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();...
1
3292
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
2765
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2199
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.