# [R] Generate random Bernoulli draws

Göran Broström gor@n@bro@trom @end|ng |rom umu@@e
Fri Jul 6 23:10:41 CEST 2018

```
On 2018-07-06 19:18, Berry, Charles wrote:
> A liitle math goes along way. See below.
>
>> On Jul 5, 2018, at 10:35 PM, Marino David
>> <davidmarino838 using gmail.com> wrote:
>>
>> Dear Bert,
>>
>> I know it is a simple question. But for me, at current, I fail to
>> implement it. So, I ask for help here.
>>
>> It is not homework.
>>
>> Best,
>>
>> David
>>
>> 2018-07-06 13:32 GMT+08:00 Bert Gunter <bgunter.4567 using gmail.com>:
>>
>>> Is this homework?
>>>
>>> (There is an informal no-homework policy on this list).
>>>
>>> Cheers, Bert
>>>
>>>
>>>
>>> Bert Gunter
>>>
>>> "The trouble with having an open mind is that people keep coming
>>> along and sticking things into it." -- Opus (aka Berkeley
>>> Breathed in his "Bloom County" comic strip )
>>>
>>> On Thu, Jul 5, 2018 at 10:23 PM, Marino David
>>> <davidmarino838 using gmail.com> wrote:
>>>
>>>> Dear All,
>>>>
>>>> I would like to generate N random Bernoulli draws given a
>>>> probability function F(x)=1-exp(-2.5*x) in which x follows
>>>> uniform distribution, say x~U(0,2).
>
> If each Bernoulli draw is based on its own draw of x, then
>
> rbinom( N, 1, 0.8013476 )
>
> is what you want.

Maybe it is what he wants, but note that F(x) as defined is a random
variable, not a (cumulative) probability function.

G,

>
> It is left as an exercise for the reader to verify that the constant
> 0.8013476 is correct up to approximation error, and to prove that
> such a Bernoulli mixture is also Bernoulli. Perhaps,
>
> ?integrate
>
> will help.
>
> But if the x's are shared you need to use runif, expm1, and
> (possibly) rep to produce a vector to be used in place of  the prob
> argument.
>
> HTH,
>
> Chuck
>
>
>>>>
>>>> Can some one leave me some code lines for implementing this?
>>>>
>>>>
>>>> David
>>>>
>>>> [[alternative HTML version deleted]]
>>>>
>>>> ______________________________________________
>>>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more,
>>>> the posting guide http://www.R-project.org/posti ng-guide.html
>>>> and provide commented, minimal, self-contained, reproducible
>>>> code.
>>>>
>>>
>>>
>>
>> [[alternative HTML version deleted]]
>>
>
> ______________________________________________ R-help using r-project.org
> mailing list -- To UNSUBSCRIBE and more, see