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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top