-2

In some coding practice websites, I found below assignment operation for generating a random whole number that falls within a range of two specific numbers.

To do this, we'll define a minimum number min and a maximum number max.

Here's the formula we'll use. Take a moment to read it and try to understand what this code is doing:

Math.floor(Math.random() * (max - min + 1)) + min

This formula is bit ambiguous to me. Could anyone please explain the logic.

uday
  • 569
  • 1
  • 6
  • 16
  • Looks like a way to generate a random whole number between (inclusive) min and max. – phuzi Jun 04 '21 at 07:52
  • `Math.random()` generates a pseudo-random number in the interval [0, 1). To get a number >= 1 you have to scale the result. – JoshG Jun 04 '21 at 07:53
  • 1
    Please read [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users). Have you read [Math.floor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor) and [Math.random](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random)? –  Jun 04 '21 at 07:54
  • If you don't know what it does, why not simply not use it? Otherwise, why not run the code and debug it? – Nico Haase Jun 04 '21 at 07:57
  • 1
    Take the formula apart and evaluate each part in the console, like `a = max - min + 1; b = Math.random() * a` etc – georg Jun 04 '21 at 08:02
  • 1
    It is explained in detail here: [Explanation of Code: Dealing with min and max - Javascript](https://stackoverflow.com/questions/39753919) – adiga Jun 04 '21 at 08:53
  • @jabaa every language/technology/tool has its own documentation end to end and books/articles related to it. So what is the purpose of stackoverflow then? Please let others reply atleast. I saw most of your comments are like this only on other posts also.Even there are other community members trying to help. – uday Jun 05 '21 at 04:58
  • 1
    Stackoverflow is not for beginners tutorials. There are hundreds online. It's for specific programming questions and _"asking a question on Stack Overflow should be the last step in your process for finding an answer"_ [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) That's how this platform works and why multiple users downvoted your question. You should probably know that multiple downvoted questions will block your account from asking new questions. –  Jun 05 '21 at 10:53
  • You should read [Why are questions no longer being accepted from my account?](https://stackoverflow.com/help/question-bans) and [ask] –  Jun 05 '21 at 10:55

1 Answers1

1

Let's break this:

  1. First lets look at Math.random(). According to here

The Math.random() function returns a pseudo-random number in the form of a floating point number in the range from 0 - 1 (including 0, but not 1)

  1. Multiply it by (max - min + 1) will give you offset in the range between min and max and will make sure you won't exceed this limit.
  2. Adding + min will make sure you get the shifting from [0 to offset] to [min to offset + min] which we said is limited by max.
  3. Finally Math.floor to make it integer number instead of float (its floor and not Math.ceil because the original Math.random() is not including the 1).
Dharman
  • 30,962
  • 25
  • 85
  • 135
Omri Attiya
  • 3,917
  • 3
  • 19
  • 35