Solved

math.random question

Posted on 2014-07-22
3
133 Views
Last Modified: 2014-07-24
I have a fairly simple question:

Please explain the following based on this:

Math.floor(Math.random()*5+1);  -- I understand this gives me a random number from 0 up to and including 5.

why is math.floor needed?
why is 5+1 needed instead of just 6?
0
Comment
Question by:Montoya
  • 2
3 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40211824
To make sure its a whole number and not something like 2.56584
.floor rounds down to the nearest whole number
.ceil rounds up to the nearest whole number
0
 
LVL 19

Author Comment

by:Montoya
ID: 40213963
why is 5+1 needed instead of just 6?
0
 
LVL 58

Accepted Solution

by:
Gary earned 300 total points
ID: 40214375
It's not 5+1 its
Math.random()*5 and then add 1

And it will be a random number between 1 and 5 not between 0 and 5
If you do want it between 0 and 5 then remove the +1
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question