[Numpy-discussion] weighted random integers

Charles R Harris charlesr.harris at gmail.com
Fri Sep 10 20:15:16 EDT 2010


On Fri, Sep 10, 2010 at 5:46 PM, <josef.pktd at gmail.com> wrote:

> I saw some questions on the web how to create non-uniform random
> integers in python.
>
> I don't know what the best way is but here is another way that looks
> reasonably fast
>
> >>> rvs = np.dot(np.random.multinomial(1, [0.1, 0.2, 0.5, 0.2],
> size=1000000),np.arange(4))
>
> >>> np.bincount(rvs)/1000000.
> array([ 0.099741,  0.199943,  0.499317,  0.200999])
>
>
This looks like a good case for the inverse cdf approach, at least for
smallish ranges of integers. Searchsorted on an array of appropriate values
should do the trick.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20100910/f657f365/attachment.html>


More information about the NumPy-Discussion mailing list