Solved

math.random question

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

910 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now