-4

I’m trying to generate a random number between 10 and 50 inclusive (the interval [10,50]) in JavaScript. I don’t need it to be an integer only, it can be any value in that interval. I know I need to use Math.random(), I’m just stuck on what the code would be to get that particular interval.

I’ve tried Math.random() * 50 + 10, but this doesn’t seem to give me values in the right interval I am looking for.

j08691
  • 204,283
  • 31
  • 260
  • 272
  • `Math.random() * 40 + 10`? It technically can't produce `50` itself (except by floating point precision error, maybe), but the odds of producing 50 itself when you have the entire floating point range between 10 and 50 to work with, and you accept any decimal component, is effectively zero, so it shouldn't matter. – ShadowRanger Sep 01 '23 at 15:31
  • 1
    This is just mathematics. `Math.random` gives you the range `[0,1)`. You multiply it by 50, resulting in the range `[0, 50)`. Then you add 10, resulting in the range `[10, 60)`. You can work backward to find the correct multiplier and adjustment so the result is `[10, 50)`. – Raymond Chen Sep 01 '23 at 15:31
  • 2
    @j08691: That's not a good duplicate, insofar as it's asking for random integers, and this question doesn't want to truncate. The rules would differ for untruncated values. – ShadowRanger Sep 01 '23 at 15:33
  • 2
    Better duplicate: [Get a random number between float numbers](https://stackoverflow.com/questions/17726753/get-a-random-number-between-0-0200-and-0-120-float-numbers). – Raymond Chen Sep 01 '23 at 15:35
  • Getting an inclusive upper bound is a bit of a problem. Because Math.random gives `[0,1)` not `[0,1]`. If you need an inclusive upper bound then you should choose a _fixed precision_, which will assign the upper bound result a non-zero probability which is uniformly distributed among the other results (equally likely as any other result). – Wyck Sep 01 '23 at 15:40
  • @Wyck: As I mentioned before, if they want untruncated values, it shouldn't matter in practice. We're talking about [something approaching `2**62` unique values that can be represented in the source range `[0, 1)`](https://stackoverflow.com/a/2978945/364696), plus one more for the inclusivity. For non-cryptographic applications, having a bias that excludes one value out of roughly `2**62` is meaningless. And for cryptographic applications, `Math.random` is inappropriate. – ShadowRanger Sep 01 '23 at 15:46
  • What about: ``const n = Math.random() * 40; const result = Math.random() < 0.5 ? 10 + n : 50 - n `` gives you a random number and then a chance to either sub from 50 or add to 10. Statistically everything covered from 10 to 50 inclusive as far as js allows – Ilja KO Sep 01 '23 at 15:46
  • What _kind_ of numbers do you need to generate? "it can be any value in that interval" isn't all that useful: what are these numbers _for_? I.e. can you please explain what problem you're working on where you've determine that a random number in a closed interval is required in the implementation. – Mike 'Pomax' Kamermans Sep 01 '23 at 17:21
  • Does this answer your question? [Random floating point double in Inclusive Range](https://stackoverflow.com/questions/9724404/random-floating-point-double-in-inclusive-range) – Peter O. Sep 01 '23 at 20:08

0 Answers0