Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# math.random question

Posted on 2014-07-22
Medium Priority
152 Views
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
Question by:Montoya
• 2

LVL 58

Expert Comment

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

ID: 40213963
why is 5+1 needed instead of just 6?
0

LVL 58

Accepted Solution

Gary earned 1200 total points
ID: 40214375
It's not 5+1 its

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

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be \$37.1B.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
###### Suggested Courses
Course of the Month14 days, 9 hours left to enroll