|  | FAQ Topic - How do I generate a random integer from 1 to N? |  | |
| | | FAQ server |  |
| Posted: Tue Sep 02, 2008 9:00 pm Post subject: 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? -----------------------------------------------------------------------
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.
LINK
LINK
How to Deal and Shuffle, see in:
LINK
-- 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 LINK The FAQ workers are a group of volunteers. The sendings of these daily posts are proficiently hosted by LINK |
| |
| | | optimistx |  |
| Posted: Wed Sep 03, 2008 2:31 am Post subject: Re: FAQ Topic - How do I generate a random integer from 1 to |  |
FAQ server wrote:
| Quote: | ----------------------------------------------------------------------- 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.
LINK
LINK
How to Deal and Shuffle, see in:
LINK
|
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. |
| |
| | | Dr J R Stockton |  |
| Posted: Wed Sep 03, 2008 4:50 pm Post subject: Re: FAQ Topic - How do I generate a random integer from 1 to |  |
| |  | |
In comp.lang.javascript message <48be132f$0$23607$f4da826c@news.24online ..fi>, Wed, 3 Sep 2008 07:31:39, optimistx <optimistxPoista@poistahotmail ..com> posted:
| Quote: | 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.
LINK
LINK
How to Deal and Shuffle, see in:
LINK
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) |
| |
| | | Guest |  |
| Posted: Wed Sep 03, 2008 5:42 pm Post subject: Re: FAQ Topic - How do I generate a random integer from 1 to |  |
| Quote: | 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.
| Quote: | Errare humanum est. I hope I made an error: then I learned something. Good point. |
|
| |
|
|