views:

239

answers:

4

Javascript's MATH object has a random method that returns from the set [0,1) 0 inclusive, 1 exclusive. Is there a way to return a truly random method that includes 1.

e.g.

var rand = MATH.random()*2;

if(rand > 1)
{
   rand = MATH.floor(rand);
}

return rand; 

While this always returns a number from the set [0,1] it is not truly random.

+4  A: 

Whether it's inclusive of the boundaries shouldn't matter; indeed, strictly speaking what you're trying to do doesn't really make sense. Remember that under a continuous probability distribution, the probability of getting a specific value is infinitesimal anyway, so mathematically speaking, you'll never see the exact value of 1.

Of course, in the world of computers, the distribution of an RNG isn't truly continuous, so it's "possible" that you'll encounter a specific value (whatever that means), but the fact that you're relying on an a limitation of how real numbers are stored hints at problems with your approach to the problem you're trying to solve.

Will Vousden
Thanks for your reply. I think it's just a poorly formed exercise that I am doing. The exercise is to write your own Math object with a random method that has takes min,max and inclusive as arguments. The inclusive argument being a boolean that says whether you want the set to be inclusive or exclusive. Now as i pointed out in my initial question that the Math.random() function is neither inclusive or exclusive becuase it inludes 0 but excludes 1. So to write a random method that is truly inclusive or exclusive and uniformly distributed is not possible, right?
Adrian Adkison
It's not impossible, since there is actually only a finite number of values that can be generated by the RNG between 0 and 1 (rather than a continuum). It is, however, mathematically nonsensical to do so. Either the exercise is referring specifically to integers, or it's a bad question. Furthermore, extending a discrete uniform distribution to form another uniform distribution is non-trivial. Trevor's and Brian McKenna's solutions are very close, but not perfectly uniform.
Will Vousden
+3  A: 

You want it to include 1?

return 1 - Math.random();

However, I think this is one of those questions which hints at other problems. Why do you need to include 1? There's probably a better way to do it.

nickf
But it won't include 0
Trevor
that would be 0 exclusive now .. wouldn't it ?
Gaby
Yes, but he didn't say that excluding 0 was a problem, only that excluding 1 was.
John Knoeller
+4  A: 

This will return [0,1] inclusive:

if(MATH.random() == 0)
    return 1;
else
    return MATH.random();

Explanation: If the first call to random() returns 0, return 1. Otherwise, call random again, which will be [0,1). Therefore, it will return [0,1] all inclusive.

Trevor
why random twice. what if you still get 0 ...
bryantsai
This will not include 0 either, as you mention in your comment to nickf
Gaby
If the first random() call isn't 0, the second random() call might be.
Trevor
I guess it's hard to say uniform, but +1 for bring 1 back in.
bryantsai
This isn't quite uniform, but it's *very* close.
Will Vousden
@Gaby This does, in face, include 0.
Justin Johnson
@Justin , yes i understood it from Trevor's follow-up comment. Thanks
Gaby
+2  A: 

The Math.random function returns a random number between 0 and 1, where 0 is inclusive and 1 is exclusive. This means that the only way to properly distribute the random numbers as integers in an interval is to use an exclusive upper limit.

To specify an inclusive upper limit, you just add one to it to make it exclusive in the calculation. This will distribute the random numbers correctly between 7 and 12, inclusive:

var min = 7;
var max = 12;
var rnd = min + Math.floor(Math.random() * (max - min + 1));
Guffa
+1 For the classic approach to defining an arbitrary random range
Justin Johnson