Still trying to figure out if there is a function in Python to get a random float value with step? Similar to randrange(start, stop, step) but for floats.
2 Answers
import random
def randrange_float(start, stop, step):
return random.randint(0, int((stop - start) / step)) * step + start
randrange_float(2.1, 4.2, 0.3) # returns 2.4
1 Comment
Mark Dickinson
Using
int(round(...)) may be a little safer than using int(...), especially if it'll usually be the case that stop - start is close to a multiple of step. Otherwise it's difficult to predict whether stop will be a possible value or not.