I was trying to use numpy.random.uniform to pick a real number on (-1, 1), but I noticed that the upper bound 1 is excluded. Is there any way to include the upper bound as well?
Asked
Active
Viewed 2,284 times
2
-
Welcome to stack overflow! What have you tried so far based on your own research? For example, in the documentation of the function, you can pass aparameters to set the exclusive `high` number for the interval, have you tried changing that? – G. Anderson Feb 06 '20 at 21:38
-
I cannot just change the high as making it say 1.1 will also allow values between 1.0 and 1.1 – malibu Feb 06 '20 at 22:01
-
If the upper bound of 1.0 were included, it would appear approximately one time in every nine thousand million million. What practical difference would that make to your application? There are many other exactly representable floating-point numbers in the range `[-1.0, 1.0]` that can never be generated by `numpy.random.uniform`, too (the float `0.1` is one of them, for example). I submit that if those missing numbers aren't a problem, then not being able to generate `1.0` is highly unlikely to be a problem either. – Mark Dickinson Feb 08 '20 at 10:23
1 Answers
1
## -------------------- uniform distribution -------------------
def uniform(self, a, b):
"Get a random number in the range [a, b) or [a, b] depending on rounding."
return a + (b-a) * self.random()
If you want to keep by definition the uniform distribution you can force the rounding up to a certain digit with the round
function, to have a chance to reach 1 by chance.
round(number, number of digits)

Julien Drevon
- 322
- 2
- 17
-
1This looks like an interesting approach! Will definitely try, but still think numpy should include an inclusive option on the high value – malibu Feb 06 '20 at 22:00
-
@learner: Why do you think NumPy should include such an option? Can you come up with a practical use-case where it would make a difference? – Mark Dickinson Feb 08 '20 at 10:28