random number generation

Brian Blais bblais at bryant.edu
Tue Aug 17 02:57:29 CEST 2010

On Aug 16, 2010, at 20:37 , Jah_Alarm wrote:

> hi,
> I need to generate a binary array with a specified average proportion
> of 1s (e.g. [1 0 0 0
> 0 1 0 0] has this proportion = 25%). In Matlab I run something like
> random(m,n)<p where p is the value
> between 0 and 1. I'm trying to use random.randint(0,2,size=[m,n]), but
> I don't understand how to specify this proportion p.

if you're coming from matlab, then you should use the numpy package  
(and you can post questions on the numpy list).  In that case, you  
can do:

from numpy import *

array([[False,  True,  True, False, False,  True, False],
        [False, False, False, False,  True, False, False],
        [ True, False, False, False, False, False,  True],
        [ True, False,  True, False, False, False, False],
        [False, False, False, False, False, False, False]], dtype=bool)

just like matlab.


Brian Blais
bblais at bryant.edu

More information about the Python-list mailing list