Aziz Al-ghannam
Aziz Al-ghannam

Reputation: 309

Why would I combine Math.floor with Math.random?

Why would anybody call Math.floor on a Math.random result? I've seen it used like:

Math.floor(Math.random() * num);

Can someone explain please?

Upvotes: 13

Views: 26579

Answers (7)

jfriend00
jfriend00

Reputation: 707496

Why would anybody call Math.floor on a Math.random result?

In a nutshell, one calls Math.floor() when you want to truncate a decimal value to its nearest integer (by just dropping the decimal portion. So, 3.9 becomes 3, 2.1 becomes 2, etc... So, you would typically use that when you need an integer and you want the integer that is smaller than or equal to the decimal value. The math library also has Math.ceil() and Math.round(). Math.ceil() gets you the next larger integer, Math.round() rounds to the nearest integer going either up or down depending upon which is closer.

I've seen it used like: Math.floor(Math.random() * num);

Breaking Math.floor(Math.Random() * num) down into it's individual pieces and explaining each piece, you get this:

Math.random() gives you a random decimal number between 0 and 1, including 0, but not including 1. So, it might give you something like 0.38548569372.

Math.random() * num gives you a random decimal number between 0 and num, including 0, but not including num. So, if num was 10, it might give you 3.8548569372.

Math.floor(Math.random() * num)) gives you a random integer number between 0 and num, including 0, but not including num. So, it might give you 3.

Math.floor() truncates the decimal number to only the integer portion. A random integer is often used for getting a random value from an array (which needs to be an integer).

Upvotes: 17

user128511
user128511

Reputation:

It's used to get an integer random number between 0 and (max - 1).

On the other hand it's faster to use | 0 as in

const randomInt = Math.random() * num | 0;

The | 0 is a binary or of 0 which the JavaScript spec effectively says the result is converted to an integer before the | happens. Note that | 0 is not the same as Math.floor. | 0 rounds to 0 whereas Math.floor rounds down.

         | 0   Math.floor         
------+------+------------
  2.5 |   2  |   2
  1.5 |   1  |   1
  0.5 |   0  |   0
 -0.5 |   0  |  -1
 -1.5 |  -1  |  -2
 -2.5 |  -2  |  -3

Upvotes: 2

Alireza
Alireza

Reputation: 104740

var num = Math.floor(Math.random() * 1000); // e.g. 885

var flNum = Math.random() * 1000; //e.g. 885.9936205333221

Try Math.random() * 1000 for example, you may get something like this: 885.9936205333221, in many cases we need a rounded number, so many developers use it with Math.floor or Math.ceil to get an integer like 885, if in your case, you don't mind having a float number, leave it as it's...

For more info about how Math.floor working, check this link:

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Math/floor

Upvotes: 1

Freesnöw
Freesnöw

Reputation: 32143

Why would I combine Math.floor With Math.random?

You combine them because otherwise it would return a float. Using Math.floor makes sure that it is a whole number inside of the range specified.

Math.random returns a flat in between 0 and 1. Multiplying it by your num or max range gets you a value with a max of that number (1 * num). So again, Math.floor is just forcing it to be a whole number.


Behind The Scenes:

RANDOM NUMBER -> .35 -> Multiplied by max (num) of 11 -> Gets 3.85 -> Math.floor(3.85) -> 3.


Keep in mind, num is the MAX + 1. Setting num to 5 will only generate numbers 1-4!


You can check out this link for more information: http://www.javascriptkit.com/javatutors/randomnum.shtml

Tada :)

Upvotes: 2

switz
switz

Reputation: 25188

Math.random() returns something like 0.8747230430599302 between [0,1)

We use .floor to round it down to the nearest integer. For example:

Math.random()*5 == 2.5889716914389282 This generates a number between [0,5).

Math.floor(Math.random()*5) == 2 //in this scenario Generates a number between [0,4]

Upvotes: 1

JCOC611
JCOC611

Reputation: 19729

Math.random() will give you a long, random decimal. What one usually does is multiply that decimal by 10, 100, 1000, etc to get a random whole number. However, since such decimal is so long, to get a absolute whole number, you use Math.floor() to round that number down.

Upvotes: 2

Mark Byers
Mark Byers

Reputation: 838386

Math.random returns a floating-point number between 0 and 1.

Returns a floating-point, pseudo-random number in the range [0, 1) that is, from 0 (inclusive) up to but not including 1 (exclusive), which you can then scale to your desired range.

Multiplying this by n gives a floating point number between 0 (inclusive) and n (exclusive).

Math.floor is then used to convert this floating point number to an integer between 0 and n - 1 (inclusive).

Upvotes: 19

Related Questions