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
49
50
# -*- 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 - 1):
weight = random()
part = weight * x
x -= part
parts.append(part)
parts.append(x)
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
#2.90807202 + 1.44236811298 + 0.435117065132 + 0.05142786447 + 0.0556912265526 + 0.00407593344896 + 0.0926021723704 + 0.00576859646479 + 0.00128938015685 + 0.00358762842417 + = 5.0
#0.234694372031 + 0.259652129056 + 0.674716978848 + 0.108044868199 + 0.831294617432 + 0.842336821512 + 0.168651478229 + 0.788686149252 + 0.904715548375 + 0.187207037067 + = 5.0