0

I need to generate a randomly filled fix-sized list of integers using only some predefined numbers to choose from. Also, the sum of the numbers in this new list has to be equal to a given number.

For example: randomly repeat these 3 numbers -2, 2, 3 to create a new list with size 20 and the sum of list items has to be 60. In this example, size = 20, sum = 60, and numbers = [-2, 2, 3] are inputs.

Can I do this in python?

5
  • 1
    Yes you can do it in python Commented Feb 14, 2019 at 12:02
  • 2
    you can if you try!!!! Commented Feb 14, 2019 at 12:03
  • @Nihal I tried, but I think maybe need a heuristic algorithm to solve that, what do think? Commented Feb 14, 2019 at 12:25
  • @AmitNanaware with a library? Commented Feb 14, 2019 at 12:26
  • @Aprilis Yes you can use random library.. Commented Feb 14, 2019 at 12:27

2 Answers 2

1
import numpy as np def sum_to_x(n, x): values = [0.0, x] + list(np.random.uniform(high=x, size=n - 1)) values.sort() return [values[i + 1] - values[i] for i in range(n)] d = sum_to_x(20, 60) print(d) print(sum(d)) 

output:

[1.1666321716192374, 3.7356682360404636, 0.10213495009527396, 15.944355221343475, 4.823749563474106, 2.771274835477797, 1.1543877569990038, 6.6231006904687675, 1.0517272997350275, 6.954578248764335, 0.9446793094979142, 2.079080332702951, 1.1813248101489293, 3.3584697034830597, 0.8048689399051554, 1.3952223726127002, 4.797652596772288, 0.30201710006423355, 0.5143495684426824, 0.29472629235259973] 60.0 

for integer values:

import random as r def random_sum_to(n, num_terms=None): num_terms = (num_terms or r.randint(2, n)) - 1 a = r.sample(range(1, n), num_terms) + [0, n] list.sort(a) return [a[i + 1] - a[i] for i in range(len(a) - 1)] print(random_sum_to(60, 20)) 

output:

[3, 3, 4, 1, 2, 5, 4, 1, 4, 2, 1, 2, 1, 2, 1, 3, 6, 1, 12, 2] 60 
Sign up to request clarification or add additional context in comments.

2 Comments

The question states that the input numbers are given as parameters; this solution does not take that into account.
i didn't noticed that,
1

You can do it, but you should know that this is a complex problem to solve it in optimal time.

I can only give you a random solution, which may take a lot of time in some cases, because it is, you know, random:

import random def create_list(possible_values, size, sum_of_values, max_iterations=10**5): for i in range(max_iterations): values = [ random.choice(possible_values) for _ in range(size)] if sum(values) == sum_of_values: # only exit once it reaches the goal print('solution found after {:,d} iterations'.format(i)) return values raise ValueError( 'no solution found after {:,d} iterations'.format(max_iterations)) 

Here is a demonstration; the same parameters may have a different durations in each invocation of the function

>>> create_list([-1, 0, 1, 2, 3], 20, 30) solution found after 38 iterations [1, 2, 1, 1, 1, 3, 2, 2, 3, 2, 3, 2, 1, 1, -1, 0, 0, 2, 2, 2] >>> create_list([-1, 0, 1, 2, 3], 20, 30) solution found after 31 iterations [2, 2, 3, 0, 3, 1, 1, 1, 3, 3, 1, 1, 0, 2, 1, -1, 3, 3, 0, 1] >>> create_list([-1, 0, 1, 2, 3], 20, 30) solution found after 93 iterations [2, -1, 2, 1, 3, 2, -1, 0, 2, -1, 3, 3, 2, 2, 3, 1, 1, 0, 3, 3] >>> create_list([-1, 0, 1, 2], 20, 30) solution found after 50,456 iterations [1, 2, 2, 2, 2, 2, 2, 2, -1, 2, 1, 0, 2, 2, 2, 2, 2, 0, 2, 1] 

If it fails, you can try again with a higher number of iterations, but it does not always solve the issue:

>>> create_list([-1, 0, 1], 20, 30) Traceback (most recent call last): File "<input>", line 1, in <module> File "/home/ralf/PycharmProjects/django_test_02/run_pw.py", line 20, in create_list 'no solution found after {:,d} iterations'.format(max_iterations)) ValueError: no solution found after 100,000 iterations >>> create_list([-1, 0, 1], 20, 30, 10**6) Traceback (most recent call last): File "<input>", line 1, in <module> File "/home/ralf/PycharmProjects/django_test_02/run_pw.py", line 20, in create_list 'no solution found after {:,d} iterations'.format(max_iterations)) ValueError: no solution found after 1,000,000 iterations 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.