How do `Math.random()` and `Math.floor()` work together?

Hello!
I’m currently taking the web development path and I’m stuck on the third part, Javascript-Built in objects. I don´t understand the Math.floor part, can anyone help me?

console.log(Math.random()*100); ( this is the first part)
Math.floor(Math.random)() *: ( this is the second part) - how can I make it a whole number?

Regards,
Regina

1 Like

Math.foor() makes it a whole number. When you want a random number (integer) the standard syntax is:

Math.floor(Math.random() * n)) //n would be the number of possible options starting
//from 0. If n was 5 the pool of random numbers would be: 0, 1, 2, 3, 4.

Here’s what happens:
First, Math.random() generates a random number between 0 and 1. Sounds strange, I know, but that’s how it works. The possibilities start at 0 and go up to, but do not include 1. I ran Math.random() 5 times logging the results to the console:

0.39994290364154983
0.28103878884356925
0.11820979926770026
0.12778879681889976
0.6060919344582774

Back to our random number syntax: Math.floor(Math.random() * n));
After Math.random() generates a floating point random number, that number is multiplied by n.
If n were 5 and the random floating point number generated were 0.6060919344582774 that result is: 3.0304596722913866.
Finally, Math.floor() comes in. The Math.random()*5 returned 3.0304596722913866, so we have Math.floor(3.0304596722913866). Math.floor()returns the largest integer less than or equal to a given number’ - quote from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor.
In this case Math.floor(3.0304596722913866) returns 3, so the random number generated by Math.floor(Math.random() * 5)) is 3 in this instance. Since Math.random() cannot generate a number >= 1, the highest possible result in our example is 4.99999999999999999. Math.floor(4.99999999999999999) will return 4.
Hope this helps!

23 Likes

Difference between:
console.log(Math.floor(Math.random()*4 +1))

and

console.log(Math.floor(Math.random()*4 )+1)

I ran both and second one tend to be more constant(not so random) than the first one.
Are they same for 1~4?

1 Like

There is no difference. The result of the evaluated expression will be the same either way you type it. Math.random() is going to return a floating point decimal between 0 and 1 (not including 1). That number will be multiplied by 4. This will result in a random floating point number between 0 and 3.9999999999999999.

In your first example, you then add 1 to the float making it between 1 and 4.9999999999999999. Math.floor() will then convert this float to highest integer that is less than the float. (Rounds down to the nearest whole number). That would give you 1, 2, 3 or 4.

In your second example, Math.floor() will convert the float returned by Math.random() to an integer value of 0, 1, 2 or 3, and then you add the 1, so you still end up with 1, 2, 3 or 4.

The randomness isn’t affected either way. Remember that for the result to be truly random, it is entirely possible to get the same number repeatedly. Every time Math.random() runs the chance for any outcome is the same as the chance for any other outcome.

6 Likes

This lesson doesn’t explain how to use the object with the console. I’ve tried every combination of parenthesis I can think of?
console.log(Math.floor(Math.random() * 100));
Get an error every time
Also, the lesson forgets that this is a beginners course, completely loses the plot by the 3rd instruction, and is incredibly poorly worded.

3 Likes

the smallest integer greater than or equal to a decimal number

This is quoted not only from the exercise text, but also from the page linked to in the instruction. We have only to peruse that page and we find those very words under, Math.ceil().

The ceiling of a number is always higher, unless that number is equivalent to an integer, such as 46.0. It’s ceiling will be 46. The ceiling of 46.0001 is 47.

4 Likes

just make it a habit that any time you use JavaScript do not forget to start with console.log
The Math lesson at hand does not do this and I too got a big red syntax error when I ran the string. I went back and added console.log to the front of the string and my return was red and error free

1 Like

The problem am running into is this. The Math.floor command, where does it go. I have tried using it in every space possible and I am not getting it. Capture

The confusing aspect of this is that Math.random() does not take an argument, but Math.floor() requires one. The argument in this case is (Math.random() * 100).

Technically it can be written with multiplication outside.

Math.floor(Math.random()) * 100

Mathematically the result is the same.

3 Likes

This is the better way of writing the code correctly…
console.log(Math.floor(Math.random()*100));
When u run it exactly the way i write it now, you will have a whole number 61

I’m new to JavaScript and after understanding how the Math object and methods work, I tried to experiment.
So I wanted to create a random number, round it and then check the number if it’s integer or not. The thing is that I want it to print the number first and after that to print if it’s True (integer) or False.
I made a command like this:

console.log(Number.isInteger(
console.log(Math.ceil(Math.random() * 100))
));

As a result, when I hit run, it does print an integer number, but for some reason it says FALSE after that. Why?
123

Thank you in advance!

Because console.log() returns undefined.

console.log(Number.isInteger(Math.floor(Math.random() * 100))

Aside

Math.ceil() will return 0 when given 0. That mean your code will return a value between 0 and 100, inclusive.

1 Like

Is it a mistake here? In the example provided there is the extra parenthesis in the end.
console.log(Math.floor(Math.random() * 50)); // Prints a random whole number between 0 and 50

console.log(
    Math.floor(
        Math.random() * 50
    )
)

We can see that all the open parens are paired with a close paren. No mistake, here.

1 Like

Really enjoyed your explanation thanks!

1 Like

(taking this alongside you so fair warning) Please tell me if I get something wrong
To clarify what happens is this
step 1 first you get math.random * 100
step 2 that output goes into math floor which rounds it down to anything between 0-99
step 3 that goes into console.log which returns a string (this goes into the code not the console)
step 4that string goes into Number.Isinteger() the string is not an integer if you do Number.Isinteger(‘9’) it will also return false
step 5 the “false” result gets put into the console.log() command which outputs “false” to the console
you can’t stack console.log commands as far as I know

Math.ceil() always rounds up, but for one case: 0 rounds up to 0 else we’d be adding 1 which is not was ceil does.

Oh my bad I was thinking of Math.floor and typed the wrong thing.

Ah, that makes sense, then. The floor of a float number is the whole number before the decimal. It’s not really rounding as much as truncating.

Now if we consider that Math.random() never generates 1 (but can generate 0), when we multiply it by N, it can be never be N, though it can be close. Flooring 0.99999999999 * 10 will still be 9 or less.

Moving on to step 3, when you’re ready…

Hi, I’m a little lost on the Intro to JS, Built-in Objects part. On #3, I typed in:

console.log(Math.ceil(43.8(Math.floor(Math.random()*100))));

and I got a check mark, but everything showed up red on the right side. A bunch of red!!!
Did I get this right or wrong? I’m thinking wrong, since everything is in red, but I don’t know how to fix it.