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/