How would I make a list of N (say 100) random numbers, so that their sum is 1?
I can make a list of random numbers with
r = [ran.random() for i in range(1,100)]
How would I modify this so that the list sums to 1 (this is for a probability simulation).
Create a list consisting of 0 and 1, then add 99 random numbers. Sort the list. Successive differences will be the lengths of intervals that add up to 1.
I'm not fluent in Python, so forgive me if there's a more Pythonic way of doing this. I hope the intent is clear though:
Here's an updated implementation in Python 3:
Dividing each number by the total may not give you the distribution you want. For example, with two numbers, the pair x,y = random.random(), random.random() picks a point uniformly on the square 0<=x<1, 0<=y<1. Dividing by the sum "projects" that point (x,y) onto the line x+y=1 along the line from (x,y) to the origin. Points near (0.5,0.5) will be much more likely than points near (0.1,0.9).
For two variables, then, x = random.random(), y=1-x gives a uniform distribution along the geometrical line segment.
With 3 variables, you are picking a random point in a cube and projecting (radially, through the origin), but points near the center of the triangle will be more likely than points near the vertices. The resulting points are on a triangle in the x+y+z plane. If you need unbiased choice of points in that triangle, scaling is no good.
The problem gets complicated in n-dimensions, but you can get a low-precision (but high accuracy, for all you laboratory science fans!) estimate by picking uniformly from the set of all n-tuples of non-negative integers adding up to N, and then dividing each of them by N.
I recently came up with an algorithm to do that for modest-sized n, N. It should work for n=100 and N = 1,000,000 to give you 6-digit randoms. See my answer at:
Create constrained random numbers?
The best way to do this is to simply make a list of as many numbers as you wish, then divide them all by the sum. They are totally random this way.
or, as suggested by @TomKealy, keep the sum and creation in one loop:
For the fastest performance, use
numpy
:And you can give the random numbers any distribution you want, for a probability distribution:
---- Timing ----
In the spirit of "divide each element in list by sum of list", this definition will create a list of random numbers of length = PARTS, sum = TOTAL, with each element rounded to PLACES (or None):
result:
generate 100 random numbers doesn't matter what range. sum the numbers generated, divide each individual by the total.
In addition to @pjs's solution we can define a function with two parameters as well.