Skip to content
Julie edited this page Jun 13, 2019 · 15 revisions

The Python script can be used as follows to create M sets each containing N random values that sum up to C:

import numpy as np

# make sure we get for this example reproducible results
np.random.seed(seed=12345)

M = 3      # number of random number sets
N = 5      # number of random numbers that need to sum up to C
C = 3.0    # value of sum of N random numbers of one set

# matrix of random sets (# rows = M, # of columns = N) 
rand = PieShareDistribution(M, N, C, remainder=True)

The matrix contains M=3 sets each containing N=5 values:

rand[0,:]    # [ 1.45478257  0.18399595  0.13153194  0.25154563  0.9781439 ]   
rand[1,:]    # [ 0.60757194  1.60624723  0.32318579  0.34674005  0.11625498]
rand[2,:]    # [ 0.87384856  1.40702379  0.00302248  0.07622537  0.63987979]

They all sum up to C=3.0:

np.sum(rand[:,:],axis=1)     # [ 3.  3.  3.]

Funded under IMPC project of Global Water Futures program.

Table of contents

Clone this wiki locally