Populate an array with integers (randomly, weighted)
![](https://secure.gravatar.com/avatar/9d961b7961171c7b97863ed8eca83f40.jpg?s=120&d=mm&r=g)
Hi, I'd like to randomly populate a numpy array (100,100) so that the numpy.sum() of the array equals exactly 300 (e.g. distribute 300 rice grains on a chess board). The final array should only contain integers (so no floats) from 0 to X (max 300). Furthermore I'd like to use a weighing factor, so that some of my array-cells are more probably to be populated than others. Therefore I have an array of the same size (100,100) and that is in numpy.sum = 1: x = numpy.random.random_sample((10,10)) y = x/numpy.sum(x) Some side conditions: Of course if one cell has exactly 0 as weighing factor, also the result has to be 0 for that cell. If only one cell has a weighing factor at all (all others are 0) the entire 300 has to be assigned to that cell. Can that easily be achieved with Scipy/Numpy? Thank you so much for your help, Johannes
![](https://secure.gravatar.com/avatar/764323a14e554c97ab74177e0bce51d4.jpg?s=120&d=mm&r=g)
On Tue, Aug 27, 2013 at 8:54 AM, Johannes Radinger < johannesradinger@gmail.com> wrote:
Hi,
I'd like to randomly populate a numpy array (100,100) so that the
(e.g. distribute 300 rice grains on a chess board). The final array should only contain integers (so no floats) from 0 to X (max 300). Furthermore I'd like to use a weighing factor, so that some of my array-cells are more probably to be populated than others. Therefore I have an array of the same size (100,100) and that is in numpy.sum = 1:
x = numpy.random.random_sample((10,10)) y = x/numpy.sum(x)
Some side conditions: Of course if one cell has exactly 0 as weighing factor, also the result has to be 0 for that cell. If only one cell has a weighing factor at all (all others are 0)
numpy.sum() of the array equals exactly 300 the entire 300 has to be assigned to that cell. You are describing a multinomial distribution: http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.multinomial... -- Robert Kern
![](https://secure.gravatar.com/avatar/6c8561779fff34c62074c614d19980fc.jpg?s=120&d=mm&r=g)
To Robert's point, the following should give you the result. Let y be this array that provides the "weighting" factor you were talking about. result = numpy.random.multinomial(300, y.ravel()).reshape(y.shape) This should be the array you are looking for. -Travis On Tue, Aug 27, 2013 at 4:03 AM, Robert Kern <robert.kern@gmail.com> wrote:
On Tue, Aug 27, 2013 at 8:54 AM, Johannes Radinger < johannesradinger@gmail.com> wrote:
Hi,
I'd like to randomly populate a numpy array (100,100) so that the
(e.g. distribute 300 rice grains on a chess board). The final array should only contain integers (so no floats) from 0 to X (max 300). Furthermore I'd like to use a weighing factor, so
be populated than others. Therefore I have an array of the same size (100,100) and that is in numpy.sum = 1:
x = numpy.random.random_sample((10,10)) y = x/numpy.sum(x)
Some side conditions: Of course if one cell has exactly 0 as weighing factor, also the result has to be 0 for that cell. If only one cell has a weighing factor at all (all others are 0)
numpy.sum() of the array equals exactly 300 that some of my array-cells are more probably to the entire 300 has to be assigned to that cell.
You are describing a multinomial distribution:
http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.multinomial...
-- Robert Kern
_______________________________________________ SciPy-User mailing list SciPy-User@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
-- Travis Oliphant Continuum Analytics, Inc. http://www.continuum.io
![](https://secure.gravatar.com/avatar/a5c6e0b8f64a8a1940f5b2d367c1db6e.jpg?s=120&d=mm&r=g)
On 8/27/2013 3:54 AM, Johannes Radinger wrote:
I'd like to randomly populate a numpy array (100,100) so that the numpy.sum() of the array equals exactly 300 (e.g. distribute 300 rice grains on a chess board).
One possibility below. Alan Isaac
wts = np.array([[3,2,0],[1,0,2]]) a = np.zeros(wts.shape) accwts = np.cumsum(wts.flat) draws = np.random.random((300)) * accwts[-1] cts = np.bincount(np.digitize(draws,accwts),minlength=wts.size) a.flat += cts a array([[ 107., 83., 0.], [ 36., 0., 74.]])
participants (4)
-
Alan G Isaac
-
Johannes Radinger
-
Robert Kern
-
Travis Oliphant