How to generate a random number in the interval [-0.5, 0.5] using Python random()
or randrange()
functions?
random returns a float and takes no arguments, randrange takes an upper and lower bound but takes and returns an int.
from random import randrange
print(randrange(-5, 5))
If you want floats use uniform:
from random import uniform
uniform(-.5, .5)
this will do the trick random.random() - .5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With