# Flip a weighted coin

**URL:** <https://discourse.julialang.org/t/flip-a-weighted-coin/36947>\
**Category:** New to Julia\
**Created:** [April 3, 2020, 10:56am UTC](https://discourse.julialang.org/t/flip-a-weighted-coin/36947 "2020-04-03T10:56:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![samantp](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@samantp](https://discourse.julialang.org/u/samantp)\
**Post date:** [April 3, 2020, 10:56am UTC](https://discourse.julialang.org/t/flip-a-weighted-coin/36947/1 "2020-04-03T10:56:38Z")

</div>

I am looking for a function/package that will let me flip a weighted coin in Julia, where I can specify the weight. e.g. some adjustable probability p, that determines the probability of heads.

I found the bitrand function in the Random package but to the best of my knowledge this does not have the option to specify a weight.  
Does anyone know if there’s a package/function that can do this?  
Thanks

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [April 3, 2020, 11:01am UTC](https://discourse.julialang.org/t/flip-a-weighted-coin/36947/2 "2020-04-03T11:01:25Z")

</div>

Maybe something like this:

```julia
julia> coin(w = 0.5) = rand() < w;

julia> coin()
false

julia> coin()
true

```

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [April 3, 2020, 11:03am UTC](https://discourse.julialang.org/t/flip-a-weighted-coin/36947/3 "2020-04-03T11:03:47Z")

</div>

or using `Bernoulli` distribution from Distributions.jl package. The benefit is that it will be easier to simulate multiple draws.

```julia
julia> using Distributions

julia> coin = Bernoulli(0.3)
Bernoulli{Float64}(p=0.3)

julia> rand(coin)
0

julia> rand(coin, 10)
10-element Array{Int64,1}:
 0
 0
 0
 0
 0
 0
 0
 0
 1
 1

```

---

<div class="post-metadata">

**Author:** ![samantp](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@samantp](https://discourse.julialang.org/u/samantp)\
**Post date:** [April 3, 2020, 11:10am UTC](https://discourse.julialang.org/t/flip-a-weighted-coin/36947/4 "2020-04-03T11:10:01Z")

</div>

Thanks, this might work, but actually what I was looking to do was flip multiple coints with _different_ weights, are there modifications of this that would work? I suppose that would need a new distribution every time so it might not be optimal.

---

<div class="post-metadata">

**Author:** ![samantp](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@samantp](https://discourse.julialang.org/u/samantp)\
**Post date:** [April 3, 2020, 11:11am UTC](https://discourse.julialang.org/t/flip-a-weighted-coin/36947/5 "2020-04-03T11:11:04Z")

</div>

Thanks, I think that this is an elegant solution. In this case if I wanted to use a vector of weights, w, I could just call it as coin.(w) and then flip multiple coins with weights corresponding to entries in w, correct?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [April 3, 2020, 11:15am UTC](https://discourse.julialang.org/t/flip-a-weighted-coin/36947/6 "2020-04-03T11:15:17Z")

</div>

Yea, but you can do that with the `Distributions` example too:

```julia
julia> coins = Bernoulli.([0.2, 0.5, 0.7]) # 3 coins with different weights
3-element Array{Bernoulli{Float64},1}:
 Bernoulli{Float64}(p=0.2)
 Bernoulli{Float64}(p=0.5)
 Bernoulli{Float64}(p=0.7)

julia> rand.(coins) # flip the set of coins
3-element BitArray{1}:
 0
 1
 1

```
