# Choosing a binary value with a certain probability (Biased random generator)

**URL:** <https://discourse.julialang.org/t/choosing-a-binary-value-with-a-certain-probability-biased-random-generator/34141>\
**Category:** Probabilistic Programming\
**Tags:** question\
**Created:** [February 4, 2020, 2:32am UTC](https://discourse.julialang.org/t/choosing-a-binary-value-with-a-certain-probability-biased-random-generator/34141 "2020-02-04T02:32:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![HoldenCaulfield](https://avatars.discourse-cdn.com/v4/letter/h/22d042/32.png) [@HoldenCaulfield](https://discourse.julialang.org/u/HoldenCaulfield)\
**Post date:** [February 4, 2020, 2:32am UTC](https://discourse.julialang.org/t/choosing-a-binary-value-with-a-certain-probability-biased-random-generator/34141/1 "2020-02-04T02:32:06Z")

</div>

I want to determine a random binary variable according to its probability. So let’s say that I have a binary variable with a probability of 70%, so I want it to become 1 with the probability of 70% and zero with a probability of 30%. How can I do it in Julia? Is there any function for that?

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [February 4, 2020, 6:09am UTC](https://discourse.julialang.org/t/choosing-a-binary-value-with-a-certain-probability-biased-random-generator/34141/2 "2020-02-04T06:09:27Z")

</div>

If you want exactly one binary variable with 70%/30% probabilities, then you can do something like this

```julia
rand() < 0.7 ? 1 : 0

```

In more general scenario, you can use [Distributions](https://juliastats.org/Distributions.jl/stable/) package

```julia
d = Categorical([0.3, 0.7])
rand(d, 100) .- 1

```

`-1` because `Categorical` distributions produces `1, 2, 3, 4...`

---

<div class="post-metadata">

**Author:** ![HoldenCaulfield](https://avatars.discourse-cdn.com/v4/letter/h/22d042/32.png) [@HoldenCaulfield](https://discourse.julialang.org/u/HoldenCaulfield)\
**Post date:** [February 4, 2020, 6:34am UTC](https://discourse.julialang.org/t/choosing-a-binary-value-with-a-certain-probability-biased-random-generator/34141/3 "2020-02-04T06:34:50Z")

</div>

Many thanks!

---

<div class="post-metadata">

**Author:** ![cpfiffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpfiffer/32/208747_2.png) [@cpfiffer](https://discourse.julialang.org/u/cpfiffer)\
**Post date:** [February 4, 2020, 2:42pm UTC](https://discourse.julialang.org/t/choosing-a-binary-value-with-a-certain-probability-biased-random-generator/34141/4 "2020-02-04T14:42:41Z")

</div>

The more succinct way to do this would be to use the `Bernoulli` distribution:

```julia
using Distributions

dist = Bernoulli(0.7)
rand(dist)

```

---

<div class="post-metadata">

**Author:** ![HoldenCaulfield](https://avatars.discourse-cdn.com/v4/letter/h/22d042/32.png) [@HoldenCaulfield](https://discourse.julialang.org/u/HoldenCaulfield)\
**Post date:** [February 4, 2020, 2:46pm UTC](https://discourse.julialang.org/t/choosing-a-binary-value-with-a-certain-probability-biased-random-generator/34141/5 "2020-02-04T14:46:49Z")

</div>

That’s very concise! Thanks!
