473,769 Members | 7,097 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()*1 0

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 5035
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()*1 0
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()*1 0


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*********@dr n.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.ntlwor ld.com/stephen.chalmer s/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*(M ath.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.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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()*1 0

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(((iM ax - 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
6127
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 gives me a double, not an int. so when i go to use the array, it needs an int. in the language spec, it tells me that round-towards-zero is used when going from floating point to integer, and the casting is done automatically if possible.
0
6309
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*) LD_LIBRARY_PATH=/net/ia64/lavasani/Python-2.3:/usr/local/lib/hpux32:/usr/local/lib CC='gcc' LDSHARED='gcc -shared' OPT='-DNDEBUG -g -O3 -Wall -Wstrict-prototypes' ./python -E ./setup.py -q build;; \
5
1975
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 (also works in vrmlscript for blaxxun Contact vrml plugin) The general idea is to make Math.random() more "random" by calling it x number of times. X would would be decimal places taken from a time stamp, but in this simplified example it is...
23
4203
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 initialize it with the same parameter. Can this be done with JavaScript? I couldn't find anything in the documentation. Basically, what I want to achieve is to obtain always the same sequence of random numbers for the same given initialization...
6
8776
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 Math.ceil/floor and parseInt +/- 1 to ensure the amount to move was at least 1 and in the right direction. I made a small test to see which one is faster and also included simply adding/subtracting 1. parseInt generally took 50% to 100% longer than...
3
17708
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 Math.random()? Thanks, Bart -- "Share what you know. Learn what you don't."
34
4112
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 distribution over that range, using an implementation-dependent algorithm or strategy." This is not good enough for my purposes (to generate identifiers with sufficient entropy, say 256 bits or so, to effectively preclude any chance of any two calls...
2
2656
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 "Math". " i have imported java.awt like so: import java.awt.*; and this is the random number statement: g.fillRect((int)(Math.random()*getSize().width),(int)(Math.random()*getSize().height),20,20); any help you can provide will be greatly...
9
5617
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 to complete this part to move onto the next. <HTML> <HEAD> <TITLE>M150 TMA 5 : Programming : Task 1 - Testing Math.random()</TITLE>
0
9423
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,...
1
9996
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
8872
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
6674
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();...
0
5304
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3963
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
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
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.