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

Home Posts Topics Members FAQ

Math.random syntax

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
4 5023
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6108
by: Murat Tasan | last post by:
here is the situation... i have an array... and i select something from random from it. i pick a random number x by using Math.random() and multiplying it by the length of the array. but this...
0
6269
by: M. Lavasani | last post by:
Hi I am trying to test Python-2.3. Any solution for this problem please: >>>gmake test case $MAKEFLAGS in \ *-s*)...
5
1964
by: R. Russell Kinter | last post by:
Hi all, First of all I am fairly new to javascript. Most of my experience has been with its subset vrmlscript, so have mercy. The following script works in I.E. 5.5, but hangs up in Netscape 4.8...
23
4150
by: Thomas Mlynarczyk | last post by:
I remember there is a programming language where you can initialize the random number generator, so that it can - if you want - give you the exactly same sequence of random numbers every time you...
6
8752
by: RobG | last post by:
I am writing a script to move an absolutely positioned element on a page by a factor using style.top & style.left. The amount to move by is always some fraction, so I was tossing up between...
3
17693
by: Bart Vandewoestyne | last post by:
Simple question: does the Java standard specify what random number generator algorithm should be used for Math.random() ? If 'no', then can anybody tell me what PRNG is behind Sun's version of...
34
4072
by: Johannes Baagoe | last post by:
About Math.random(), ECMA 262 just says "Returns a number value with positive sign, greater than or equal to 0 but less than 1, chosen randomly or pseudo randomly with approximately uniform...
2
2639
by: Helmer | last post by:
i am in programming class trying to make an applet involving random placement of colored blocks, but an error is created when i try to run it saying "no method named "random" was found in type...
9
5581
by: DaiOz | last post by:
Hi guys im doing a course which includes some JavaScript and I need help with the following question please, The code I have so far is below but I don't think its right please help as I need...
0
7205
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
7093
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...
0
7287
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
7467
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...
0
5594
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,...
1
5022
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...
0
4688
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...
0
3177
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
746
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.