Me gustaría saber cómo puedo poner límites a cada número aleatorio obtenido, que cada número no sea mayor que 4:
Ejemplo: (4, 3, 4, 4, 4) sum = 19
Ejemplo2: (2, 2, 4, 4, 4) sum = 16
#! /usr/bin/env python
from random import sample
from itertools import izip
def slice_lengths(length=19, cuts=4):
"""Take a theoretical ribbon and slice it cuts number of times.
The return value is a generator of the different lengths made.
Parameters:
length, a number greater than zero
cuts, a number less than length
"""
# Calculate where the positions of the ribbon edges will be.
edge = [0] + sorted(sample(xrange(1, length), cuts)) + [length]
# Deltas for a moving window of two values across edge.
return ((b - a) for a, b in izip(edge, edge[1:]))
if __name__ == "__main__":
s = tuple(slice_lengths())
print s, "sum =", sum(s)
cuts
y que a su vez el conjunto sumelength
?