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

Robert Kern robert.kern at gmail.com
Tue Aug 27 05:03:36 EDT 2013


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20130827/661b044a/attachment.html>


More information about the SciPy-User mailing list