Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,924 questions

51,857 answers

573 users

How to divide a number into k random numbers in Python

2 Answers

0 votes
import numpy as np

num, k = 156, 5

divides = np.random.default_rng().multinomial(num, [1 / k] * k, size = 1)[0]

print(np.sum(divides), divides)

 
 
 
 
 
'''
run:
 
156 [27 28 39 30 32]
 
'''

 



answered Feb 21, 2023 by avibootz
0 votes
import numpy as np

num, k = 156, 5

lst = np.random.default_rng().dirichlet(np.ones(k), size = 1)

divides = [round(val) for val in lst[0] * num]

print(np.sum(divides), divides)

 
 
 
 
 
'''
run:
 
156 [17, 12, 56, 64, 7]
 
'''

 



answered Feb 21, 2023 by avibootz

Related questions

1 answer 96 views
1 answer 166 views
1 answer 96 views
1 answer 159 views
...