10

What is the easiest way to include 1 using Math.random function?

Since the default is number between inclusive 1 and exclusive 0.

I am going to use my the rand function a lot of times in my code that is why i need an even probability of getting numbers between 0 and 1 [0,1] , inclusive 0 and inclusive 1. Thanks!

Edit: Sorry, big mistake. Exclusive 1, inclusive 0.

Mika Sa
  • 143
  • 3
  • 13
  • 4
    One could ask, *why*? If you have a problem that hinges on the difference between 1 and the next lowest floating point number there are probably much more significant problems you should be focusing on. – Sinkingpoint Jan 22 '14 at 19:51
  • 1
    `What is the easiest way to include 1 using Math.random function? Since the default is number between inclusive 1 and exclusive 0.` So by default 1 is included. So what's the question? – Taylor Jan 22 '14 at 19:54
  • 3
    Careful on the "duplicate" close: this is *not* a duplicate of "generating [integer] numbers in a range" as that relates to returning 0 *or* 1, but not real numbers in [0..1]. – user2864740 Jan 22 '14 at 19:54
  • 4
    @taylor he misspoke (mis-wrote?) 1 is excluded, 0 is included. Math.random `Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. ` – JVMATL Jan 22 '14 at 19:56
  • 3
    Look at this [answer](http://stackoverflow.com/a/2143750/597419), I think it will help you understand why you don't need an inclusive random function. If you truly do need an inclusive random, look at the answer below that one. – Danny Jan 22 '14 at 20:03
  • 1
    Technically the probability of getting 1 out of [0,1] (assuming doubles) is very small and unless you try billions of time you are unlikely to get 1 anyway... – assylias Jan 22 '14 at 20:08
  • I'd say closest would be `Math.random() * Math.nextUp(1.)` – mata Jan 22 '14 at 20:34
  • @Quirliom, Im using it to test the performance of an algorithm (I know Javascript isnt the best) – Mika Sa Jan 24 '14 at 04:15
  • Sorry late replies. @Quirliom, Im using it to test the performance of an algorithm (I know Java isnt the best languange in code performance testing but i had to), and in multiple parts of the algorithm it encounters an equation which ask for a random number [0,1]. So i kinda guess that the probability should be equal since performance is tested. – Mika Sa Jan 24 '14 at 04:26
  • @Taylor, sorry that was a typo. – Mika Sa Jan 24 '14 at 04:27
  • @Danny, Thanks, yes, i think the answer below your link is the closest one. – Mika Sa Jan 24 '14 at 04:27
  • @assylias, yes im using doubles, i know its unlikely,but id call it multiple times, not really billions, more like 10 thousand plus times in one run, and i have multiple runs of the algorithm. So il be using this rand a lot. – Mika Sa Jan 24 '14 at 04:29
  • It won't let me post an answer because this is marked as a duplicate of a question that it clearly isn't a duplicate of, but I did something like double x = 2 * Math.random(); if (x >= 1) x = 2 - x; I tried testing it, but even after tens of thousands of iterations I couldn't get either a 0 or a 1. – Zac Jun 19 '17 at 15:07
  • Here is a solution I came up with: https://stackoverflow.com/a/61718928/1001290 – Sjieke May 10 '20 at 21:24

0 Answers0