Re: Random number generator

2009-04-09 Thread Jim Gibson
On 4/9/09 Thu  Apr 9, 2009  2:34 PM, ANJAN PURKAYASTHA
anjan.purkayas...@gmail.com scribbled:

 I need a Bernoulli random number generator which takes as inputs: a value
 for p(1) (probability of choosing a 1) and the number of trials and
 outputs a series of 0s and 1s according to the model B(p(1)).
 Any thoughts on which module I need to use?

I am not sure what a Bernoulli random number generator is, but I am guessing
it is an algorithm that generates values 0 or 1 based on a probability of
picking a 1. If that is not correct, ignore the following (:^).

Do you need a module? It would seem easy enough to generate an array of
true/false values:

sub bernoulli
{
my( $p, $n ) = @_;
return map { (rand()  $p) } (1..$n);
}

or explicit 0s and 1s:

sub bernoulli
{
my( $p, $n ) = @_;
return map { (rand()  $p) ? 1 : 0 } (1..$n);
}

 or you can just return a 0 or 1 on each call:

sub bernoulli
{
my( $p, $n ) = @_;
return (rand()  $p) ? 1 : 0;
}

(but then you don't need to pass the routine the number of trials).




-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Random number generator

2009-04-09 Thread Chas. Owens
On Thu, Apr 9, 2009 at 17:34, ANJAN PURKAYASTHA
anjan.purkayas...@gmail.com wrote:
 I need a Bernoulli random number generator which takes as inputs: a value
 for p(1) (probability of choosing a 1) and the number of trials and
 outputs a series of 0s and 1s according to the model B(p(1)).
 Any thoughts on which module I need to use?
snip

When you need something the first place to look is CPAN[1].  The
Math::GSL[2] module has a function sounds like it fits your needs:

gsl_ran_bernoulli($r, $p) - This function returns either 0 or 1, the
result of a Bernoulli trial with probability $p. The probability
distribution for a Bernoulli trial is, p(0) = 1 - $p and p(1) = $p. $r
is a gsl_rng structure.

gsl_ran_bernoulli_pdf($k, $p) - This function computes the probability
p($k) of obtaining $k from a Bernoulli distribution with probability
parameter $p, using the formula given above.

1. http://search.cpan.org/
2. http://search.cpan.org/dist/Math-GSL/lib/Math/GSL/Randist.pm


-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Random number generator

2009-04-09 Thread ANJAN PURKAYASTHA
Thank you guys.
Anjan

On Thu, Apr 9, 2009 at 6:59 PM, Chas. Owens chas.ow...@gmail.com wrote:

 On Thu, Apr 9, 2009 at 17:34, ANJAN PURKAYASTHA
 anjan.purkayas...@gmail.com wrote:
  I need a Bernoulli random number generator which takes as inputs: a value
  for p(1) (probability of choosing a 1) and the number of trials and
  outputs a series of 0s and 1s according to the model B(p(1)).
  Any thoughts on which module I need to use?
 snip

 When you need something the first place to look is CPAN[1].  The
 Math::GSL[2] module has a function sounds like it fits your needs:

 gsl_ran_bernoulli($r, $p) - This function returns either 0 or 1, the
 result of a Bernoulli trial with probability $p. The probability
 distribution for a Bernoulli trial is, p(0) = 1 - $p and p(1) = $p. $r
 is a gsl_rng structure.

 gsl_ran_bernoulli_pdf($k, $p) - This function computes the probability
 p($k) of obtaining $k from a Bernoulli distribution with probability
 parameter $p, using the formula given above.

 1. http://search.cpan.org/
 2. http://search.cpan.org/dist/Math-GSL/lib/Math/GSL/Randist.pm


 --
 Chas. Owens
 wonkden.net
 The most important skill a programmer can have is the ability to read.




-- 
=
anjan purkayastha, phd
bioinformatics analyst
whitehead institute for biomedical research
nine cambridge center
cambridge, ma 02142

purkayas [at] wi [dot] mit [dot] edu
703.740.6939