[SciPy-User] Populate an array with integers (randomly, weighted)

Travis Oliphant travis at continuum.io
Tue Aug 27 14:27:23 EDT 2013


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 at gmail.com> wrote:

> On Tue, Aug 27, 2013 at 8:54 AM, Johannes Radinger <
> johannesradinger at gmail.com> wrote:
> >
> > 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.
>
> You are describing a multinomial distribution:
>
>
> http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.multinomial.html
>
> --
> Robert Kern
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>


-- 

Travis Oliphant
Continuum Analytics, Inc.
http://www.continuum.io
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20130827/d1401304/attachment.html>


More information about the SciPy-User mailing list