0

i am using this function to generate random number between 1000 and 100. but here according to me, in (max - min) + min, max- min =900 and min= 100, so it should not generate numbers between 900 and 100? but it is returning numbers greater than 900 also how? I am confused. and do tell how to check the range for the numbers random function is generating? any help with this?

  x = Math.floor(Math.random() * (1000 - 100) + 100);
  console.log(x);
Rahul Syal
  • 545
  • 2
  • 6
  • 21

5 Answers5

1

The formula for random numbers Math.random() * (max - min) + min is the correct one to get a uniformly distributed number between min and max.

max - min will give you the range in which you want to generate the random numbers. So in this case 1000 - 100 results in a range of 900.

Multiplying by Math.random() will give you a random number in the range. So, with a Math.random() producing 0.5 after multiplying you get 450.

Finally, adding min back to the random pick ensures the number you get is within bounds of min and max.

For example Math.random() produces 0.01 if we substitute in the formula we get 0.01 * (1000 - 100) = 9 which is below min. Conversely, if Math.random() produces 1 then 1 * (1000 - 100) = 900 which is the highest random number possible to get from the range and yet it's still below max. In both cases adding min to the result ensures the random number you get is within max and min

VLAZ
  • 26,331
  • 9
  • 49
  • 67
0

Multiply by (1000 -200) instead as you already have +100

Because in case random number generated is anything greater than 800 you end exceeding range as you're adding 100 in it everytime

x = Math.floor(Math.random() * (1000 - 200) + 100);
console.log(x);

Thumb rule :-

  Math.floor(Math.random() * - ( max - ( 2 * min ) ) + min )
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
0

function random(min, max) {
  console.log("Multiplying by: " + (max - min));
  console.log("And adding : " + min);
  return Math.floor(Math.random() * (max - min) + min);
}

console.log(random(100, 1000));
nick zoum
  • 7,216
  • 7
  • 36
  • 80
0

The function Math.random() returns a number between 0 and 1.

When use "Math.random() * (1000 - 100)", this part of the code generates a number between 0 and 1 then multiplies it by 900, which will give you a number between 0 and 900.

Now in the last block you do add 100 to the previously generated number which results in a number between 0 and 900 + 100, which gives a result between 100 and 1000.

Kunj
  • 1,980
  • 2
  • 22
  • 34
iliasse
  • 247
  • 1
  • 7
0

As Math.random() generate floats, this need to be converted to an integer.

We can use parseInt(), but there is a shorthand, the ~~ bitwise operator. Performances are known to be excellent.

console.log(

 100 + ~~(Math.random() * 800)
  
)

One possible alternative is the web crypto api, it might be a bit slower, but with the best randomness doable. This return an integer between 0 and 256.

console.log(

  100 + ~~(crypto.getRandomValues(new Uint8Array(1))[0] * 3.13)
      
)
NVRM
  • 11,480
  • 1
  • 88
  • 87