lundi 16 septembre 2019

Python - Generate list of random number with the sum divisible by 2

My concern was what is a good way to generate a list of 10 - 15 numbers that the sum of them always divisible by n.

Here what I got so far....

import random
import numpy as np

def get_num(x, y, n):
    return [random.choice(range(x, y, n)) if x % n != 0 else random.choice(range(x - (x % n) + n, y, n)) for x in x]


def get_list():
    numb = get_num(0, 9, 2)
    return ''.join(np.random.multinomial(numb, [1/10], size=1))

while True:
    amount = int(input("How many Do you want to generate?" + "\n"))
    for i in range(1,amount):
        get_list()




Aucun commentaire:

Enregistrer un commentaire