Solved

math.random question

Posted on 2014-07-22
3
129 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

777 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