random number generation

J

Jah_Alarm

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.

thanks,

Alex
 
B

Brian Blais

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 *
random.rand(5,7)<0.25

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.


bb
 
R

Raymond Hettinger

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.

Try this:
from random import random
[1 if random() < 0.25 else 0 for i in range(20)]
[0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0]


Raymond
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,049
Members
48,769
Latest member
Clifft

Latest Threads

Top