Oct-03-2021, 01:02 PM
(This post was last modified: Oct-03-2021, 02:58 PM by deanhystad.)
To avoid 0 I would use random.sample(). It guarantees each value is unique.
import random def random_divisions(n, total_size=100): values = sorted([0, total_size] + random.sample(range(total_size), n-1)) return sorted([(y-x)/total_size for x,y in zip(values , values [1:])]) r = random_divisions(5) print(f"{r} {sum(r)}")
Output:[0.03, 0.04, 0.24, 0.33, 0.36] 1.0