473,406 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

FAQ Topic - How do I generate a random integer from 1 to N? (2008-09-03)

-----------------------------------------------------------------------
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
x-1 inclusive; use ` Random(N)+1 ` for 1 to N.

http://msdn2.microsoft.com/en-us/library/41336409.aspx

http://docs.sun.com/source/816-6408-10/math.htm

How to Deal and Shuffle, see in:

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. The sendings of these
daily posts are proficiently hosted by http://www.pair.com.

Sep 2 '08 #1
3 1244
FAQ server wrote:
-----------------------------------------------------------------------
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
x-1 inclusive; use ` Random(N)+1 ` for 1 to N.

http://msdn2.microsoft.com/en-us/library/41336409.aspx

http://docs.sun.com/source/816-6408-10/math.htm

How to Deal and Shuffle, see in:

http://www.merlyn.demon.co.uk/js-randm.htm
Excuse me, Mr/Mrs FAQ server, is that really correct? When
I try e.g. x = 1.5, do I really get evenly distributed results?

Errare humanum est. I hope I made an error: then I learned
something.
Sep 3 '08 #2
>
Excuse me, Mr/Mrs FAQ server, is that really correct? When
I try e.g. x = 1.5, do I really get evenly distributed results?
The argument, x, must be a positive integer.

If you want a real number between 0 and 1.5 then use x=1.5 but don't use
the floor function.

>Errare humanum est. I hope I made an error: then I learned
something.
Good point.
Sep 3 '08 #3
In comp.lang.javascript message <48***********************@news.24online
..fi>, Wed, 3 Sep 2008 07:31:39, optimistx <optimistxPoista@poistahotmail
..composted:
>FAQ server wrote:
>-----------------------------------------------------------------------
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
x-1 inclusive; use ` Random(N)+1 ` for 1 to N.

http://msdn2.microsoft.com/en-us/library/41336409.aspx

http://docs.sun.com/source/816-6408-10/math.htm

How to Deal and Shuffle, see in:

http://www.merlyn.demon.co.uk/js-randm.htm

Excuse me, Mr/Mrs FAQ server, is that really correct? When
I try e.g. x = 1.5, do I really get evenly distributed results?

Errare humanum est. I hope I made an error: then I learned
something.
ISTR that a previous FAQ maintainer was told that the argument of Random
was intended to be integer, but did not act on that (ICBW).

He also wanted to put a lower limit of 2 on x !

"therefore" should be replaced by "therefore, with positive integer x,".

However, one can use the function with other arguments, if you find it
useful.
The Microsoft and Sun references do not explicitly say that the number
is evenly distributed (I believe its evenness in imperfect in IE).

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "" (SonOfRFC1036)
Sep 3 '08 #4

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

Similar topics

3
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I generate a random integer in ? ----------------------------------------------------------------------- ...
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I generate a random integer from 1 to N?...
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I generate a random integer from 1 to N?...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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,...
0
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...

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.