By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,247 Members | 1,057 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,247 IT Pros & Developers. It's quick & easy.

Math.random syntax

P: n/a
Hi.

Reading about the Math.random method I saw that by default it
generates between 0 and 1. To generate numbers between a greater range
I can use these syntaxes:

x = Math.random()/10
x = Math.random()*10

What is the difference between the two? I could not understand the
role of the operators here.

Thanks,
Robert Scheer
Jul 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Robert Scheer wrote:
Hi.

Reading about the Math.random method I saw that by default it
generates between 0 and 1. To generate numbers between a greater range
I can use these syntaxes:

x = Math.random()/10
Divides the return from random() by 10, giving you a decimal form
between 0 and .1

x = Math.random()*10
Gives you a decimal integer between 0 and 10 by multiplying.
What is the difference between the two? I could not understand the
role of the operators here.


One divides, one multiplies.

<URL: http://www.jibbering.com/faq/#FAQ4_22 />
might be a good resouce for you, the FAQ in its entirety but especially
that section.
--
Randy

Jul 20 '05 #2

P: n/a
Lee
Randy Webb said:

Robert Scheer wrote:
Hi.

Reading about the Math.random method I saw that by default it
generates between 0 and 1. To generate numbers between a greater range
I can use these syntaxes:

x = Math.random()/10


Divides the return from random() by 10, giving you a decimal form
between 0 and .1

x = Math.random()*10


Gives you a decimal integer between 0 and 10 by multiplying.


No. It's not an integer.

The correct way to generate a "random" integer is described at:
http://www.jibbering.com/faq/#FAQ4_22

Jul 20 '05 #3

P: n/a
JRS: In article <bu*********@drn.newsguy.com>, seen in
news:comp.lang.javascript, Lee <RE**************@cox.net> posted at Thu,
15 Jan 2004 12:08:16 :-

The correct way to generate a "random" integer is described at:
http://www.jibbering.com/faq/#FAQ4_22


According to someone trustworthy, that method is not entirely reliable.
From a linked page :

Opera (5.02..6.01 at least), I have read, can give a value of 1.0
from its Math.random(), with a frequency of the order of one
in 35000 times - so that the function Random() below can
return N. There is a
<http://homepage.ntlworld.com/stephen.chalmers/op6/random1.htm> Test
age by SC. Precautions are needed; appending %1 to Math.random()
should do. LRN 20030804 : Opera 4, 5, not 6.05.

Sometimes, returning N may not much matter; other times, it may cause
the page logic to fail, resulting in error but not necessarily obvious
error.

I do not know which values in 0..N-1 that 1/35000 probability is
extracted from (it is transferred to 0, of course, by %1). If a 1/35000
non-uniformity matters, ISTM that one should perhaps be using a better
algorithm and maybe another language.

function Random(N) {
do { T = Math.random() } while (T>=1.0) ; return Math.floor(N*T) }

may be better than

function Random(N) { return Math.floor(N*(Math.random()%1)) }
--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 20 '05 #4

P: n/a
On 15 Jan 2004 06:11:26 -0800, rb******@my-deja.com (Robert Scheer)
wrote:
Hi.

Reading about the Math.random method I saw that by default it
generates between 0 and 1. To generate numbers between a greater range
I can use these syntaxes:

x = Math.random()/10
x = Math.random()*10

What is the difference between the two? I could not understand the
role of the operators here.


At the moment I use the following function to get a randum "integer"
number, I'm new to Javascript so not sure if this is the best way...
its the way I did it in VBscript.

function RandomNumber (iMin, iMax) {
return Math.floor(((iMax - iMin + 1) * Math.random()) + iMin);
}
HTH

Al.

Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.