Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include upper bound on numpy random.uniform [duplicate]

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?

like image 895
malibu Avatar asked Feb 25 '26 16:02

malibu


1 Answers

## -------------------- 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)
like image 96
Julien Drevon Avatar answered Feb 27 '26 06:02

Julien Drevon