coding utf-8 from random import seed random def rand_part_1 number par

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# -*- coding: utf-8 -*-
from random import seed, random
def rand_part_1(x, n):
"x - number, n - part count"
seed()
parts = []
for i in range(0, n):
weight = random()
part = weight * x
x -= part
parts.append(part)
return parts
def rand_part_2(x, n):
"x - number, n - part count"
seed()
parts = []
for i in range(0, n):
parts.append(random())
s = sum(parts)
for i in range(0, n):
parts[i] *= x / s
return parts
if __name__ == '__main__':
x = float(raw_input("Enter number: "))
n = int(raw_input("Enter parts count: "))
parts_1 = rand_part_1(x, n)
sum_1 = sum(parts_1)
for i in range(0, n):
print parts_1[i], ' + ',
print ' = ', sum_1
parts_2 = rand_part_2(x, n)
sum_2 = sum(parts_2)
for i in range(0, n):
print parts_2[i], ' + ',
print ' = ', sum_2
#Enter number: 5
#Enter parts count: 10
#0.724397840514 + 0.864080581531 + 2.83876459549 + 0.253212325318 + 0.15847715138 + 0.153255528692 + 0.00300477682509 + 0.00336255524947 + 0.000426331894112 + 0.000524482284921 + = 4.99950616918
#0.0148136779586 + 0.34764561011 + 1.00407211725 + 0.0647163032313 + 0.915063503618 + 0.470163509838 + 1.02061204438 + 0.108516794011 + 0.920641906705 + 0.133754532901 + = 5.0