Is
p = rand(-1.:eps():1., 100000)
a good way to get random Float values in [-1, 1]?
A common suggestion seems to be 2. * rand(100000) - 1.
instead, but
- this doesn't ever return 1 since
rand
's range is[0, 1)
- this skips out on a lot of values: let's say
eps() == 0.1
for argument's sake, thenrand
returns from (0.1, 0.2, 0.3, ..., 0.9), and after this computation you get results from (-0.8, -0.6, -0.4, ..., 0.8), so the result is not uniformly random in the range anymore.
(Note: Performance-wise, my version at the top seems to be 4x slower than the latter one. )
What is the generally recommended way of getting a uniformly random floating point number in a given range?