最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Order of operations for Math.floor(Math.random() * 5 + 1)? - Stack Overflow

programmeradmin1浏览0评论

In the Code Academy JS course, Dragon Slayer 2/6, the following text is used in the hint to describe the order of operations for the code I included in the title.

How does this code work?

Math.floor(Math.random() * 5 + 1);

  • First we use Math.random() to create a random number from 0 up to 1. For example, 0.5

  • Then we multiply by 5 to make the random number from 0 up to 5. For >example, 0.5 * 5 = 2.5

  • Next we use Math.floor() to round down to a whole number. For example, >Math.floor( 2.5 ) = 2

  • Finally we add 1 to change the range from between 0 and 4 to between 1 and >5 (up to and including 5)

I've looked this up in several different places (here and here), and a majority of them either focus on the range that Math.random() produces (which I understand) or confirm the order of operations outlined in the hint, wherein "Math.floor" acts upon "Math.random()*5" prior to the "+1" being added.

It seems to me however that, according to the order of operations that I learned in school, the last two steps should be flipped. Would that not be the case since "Math.random()*5" and the "+ 1" are both within the parenthesis?

While the difference between these two might not make a difference in the value returned from this particular code, I could see a fundamental change in the order of operation like the one outlined here would cause me some frustration further down the road if I didn't know it.

In the Code Academy JS course, Dragon Slayer 2/6, the following text is used in the hint to describe the order of operations for the code I included in the title.

How does this code work?

Math.floor(Math.random() * 5 + 1);

  • First we use Math.random() to create a random number from 0 up to 1. For example, 0.5

  • Then we multiply by 5 to make the random number from 0 up to 5. For >example, 0.5 * 5 = 2.5

  • Next we use Math.floor() to round down to a whole number. For example, >Math.floor( 2.5 ) = 2

  • Finally we add 1 to change the range from between 0 and 4 to between 1 and >5 (up to and including 5)

I've looked this up in several different places (here and here), and a majority of them either focus on the range that Math.random() produces (which I understand) or confirm the order of operations outlined in the hint, wherein "Math.floor" acts upon "Math.random()*5" prior to the "+1" being added.

It seems to me however that, according to the order of operations that I learned in school, the last two steps should be flipped. Would that not be the case since "Math.random()*5" and the "+ 1" are both within the parenthesis?

While the difference between these two might not make a difference in the value returned from this particular code, I could see a fundamental change in the order of operation like the one outlined here would cause me some frustration further down the road if I didn't know it.

Share Improve this question edited May 23, 2017 at 12:04 CommunityBot 11 silver badge asked Dec 10, 2015 at 1:53 tim_stufftim_stuff 1438 bronze badges 2
  • 3 The explanation provided for that expression is wrong. – zerkms Commented Dec 10, 2015 at 1:58
  • 2 Everything inside the parentheses wrapping Math.floor's argument should return a float number. So the order should be: Math.random() which returns a float between 0 and 0.9999... Then multiply that by 5, then add the 1. Then finally Math.floor it – philip yoo Commented Dec 10, 2015 at 1:58
Add a ment  | 

4 Answers 4

Reset to default 6

Math.floor() will work on whatever is inside the brackets, after it has been calculated.

Math.floor(Math.random() * 5 + 1)

is the same as

var i = Math.random() * 5;
i += 1;
Math.floor(i);

You are correct that the wording on the page is wrong. The last thing that will happen is the floor call. Everything in the parenthesis will be processed first.

Honestly, I think they mixed up here, and you're right. According to PEMDAS and any mathematics I've ever learned, the +1 es before the Math.floor function.

The Math.random() function returns a random number in the range [0, 1) that is, from 0 (inclusive) up to but not including 1 (exclusive). It can be any thing like 0,.34,.42 etc. if you want random number between 0-5. you will used Math.Random()*5. This will give you any number like 0,4.43.4.34 but not five. Then we add 1 like this Math.random() * 5 + 1. Now the chances is you will get a number which is between 0 and 6. But you don't want number above 5. so you apply floor method which will return largest integer less than or equal to a given number.

发布评论

评论列表(0)

  1. 暂无评论