2

In real probability, there is a 0% chance that a random number p, selected from all of the real numbers in the interval (0,1), will be 0.5. However, what are the odds that

rand == 0.5

in MATLAB? I suppose this is like asking how many double-precision numbers are between zero and one, or maybe there are other factors at play.

Doubt
  • 1,163
  • 2
  • 15
  • 26
  • See: [How many double numbers are there between 0.0 and 1.0?](http://stackoverflow.com/questions/2978930/). However, if I recall correctly, the Mersenne Twister algorithm calculates based on [1,2) and then shifts. – horchler Jul 10 '13 at 15:54
  • I may be wrong about this (can't quite remember from a long time ago), but I believe that the probability that `p` will equal 0.5 is actually undefined, not 0. When defining distributions over uncountable sets such as a real interval, the only probabilities that are defined are for when `p` is a member of some subset `s` of the interval, where `s` must have a (Lebesgue?) measure greater than zero. Sorry that doesn't directly answer your main question about doubles, but maybe it helps to think in that context. – Sam Roberts Jul 10 '13 at 16:06
  • @SamRoberts: The proper terminology from [sigma algebra](http://en.wikipedia.org/wiki/Sigma-algebra) for this is "[almost surely](http://en.wikipedia.org/wiki/Almost_surely)," commonly abbreviated a.s. – horchler Jul 10 '13 at 16:09
  • @SamRoberts: well, [at least a few people say that it's actually well-defined, and that the probability is indeed zero](http://math.stackexchange.com/questions/155156/)... – Rody Oldenhuis Jul 10 '13 at 16:13
  • @horchler Now it's starting to come back - thank you, I shall enjoy reminding myself about what sigma algebras are! – Sam Roberts Jul 10 '13 at 16:14
  • It's further complicated because most PRNG's are actually based on integers (for precision reasons), and then scaled to rationals. If the divisor is odd then 1/2 is mathematically impossible. But then in converting to a floating point there's usually some approximating in getting to the nearest possible float/double representation, so 0.5 may become possible again because of the inexact mapping. Without knowing the exact specification for MATLAB's generator and the specifics of the rational -> floating point conversion, you can't say for sure whether or not 0.5 is a possible outcome. – pjs Jul 10 '13 at 16:34

3 Answers3

1

No particular info on MATLAB's generator...

In general even simple pseudo-random generators have long enough cycles which would cover all values representable by double.

If MATLAB uses some other form of generating random numbers it would be even better - so assuming it uniformly covers whole range of double values.

I believe probability would be: distance between representable numbers around values you are interested divided by length of the interval. See What is the minimal step in double data type? (.NET) for discussion on the distance.

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
1

Looking at this question, we see that there are 262 - 252 doubles in the interval (0 1). Therefore, the probability of picking any single one (like 0.5) would be roughly equal to one divided by this number, or

>> p = 1/(2^62-2^52) 
ans =
    2.170523997312134e-019

However, as horchler already indicates, it also depends on the type of random number generator you use, as well as MATLAB's implementation thereof. Sadly, I have only basic knowledge on the implementaion details for each, but you can look here for a list of available random number generators in MATLAB and google a bit further for more precise numbers.

Community
  • 1
  • 1
Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
  • Note that most of the points between 0 and 1 will be closer to 0 than to 1, thus I guess your estimate is more of a lower bound. – Dennis Jaheruddin Jul 11 '13 at 12:19
  • @DennisJaheruddin: That is true...Does the Mersienne Twister pick any `double` from (0,1) with equal probability, or any `number`? If the first is the case, my answer is correct. But I indeed expect the *second* case to be the algorithm's aim, therefore, you'd have to look at the two separate intervals `(0,X)` and `(X, 1)` (with `X = 0.5` in this case) and rework the numbers. – Rody Oldenhuis Jul 11 '13 at 12:25
0

I am not sure whether Alexei was trying to say this, but inspired by him I think the probability will indeed be approximately the distance between numbers around 0.5.

Therefore I expect the probability to be approximately:

eps(0.5) 

Which evaluates to 1.1102e-16


Given the monotonic nature of the difference between double numbers I would actually think this holds:

eps(0.5-eps(0.5)) <= yourprobability  <= eps(0.5) 

Implying a range of 5.5511e-17 to 1.1102e-16

Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122