I'm trying to create a list (call it: weights) of N random numbers between 0.005 and 0.045 with a total sum equal to 1. N can be any integer between 22 and 200. So the following restrictions:
- Number of numbers in weights = N
- For every n in weights: 0.005 < n < 0.045
- sum of all n's in weights = 1
The first restriction is easy I think. Also, I know how to fix both the second and the third restriction separate from each other. But I don't know how to combine them into one piece of code.
- Second restriction: 0.005 < x < 0.045:
import numpy as np
import random
weights_step1 = np.random.randint(min=5, max = 45, size = N)
weights = []
for weight in weights_step1:
weights.append(weight/1000)
- Third restriction
Generating a list of random numbers, summing to 1
Does anyone know how to get both restrictions into one piece of code?