# Creating a List of 100 Binomial Distributed Values

**URL:** <https://discourse.julialang.org/t/creating-a-list-of-100-binomial-distributed-values/92903>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 13, 2023, 3:54am UTC](https://discourse.julialang.org/t/creating-a-list-of-100-binomial-distributed-values/92903 "2023-01-13T03:54:49Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![BadBoy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/badboy/32/202815_2.png) [@BadBoy](https://discourse.julialang.org/u/BadBoy)\
**Post date:** [January 13, 2023, 3:54am UTC](https://discourse.julialang.org/t/creating-a-list-of-100-binomial-distributed-values/92903/1 "2023-01-13T03:54:49Z")

</div>

I want a list x it contain 1 and -1, if rand(Binomial(1,0.5),1)[1] == 1 is true the value in that position of x is 1 and if it is not true then the value is -1 . i need to do this for 100 many times, so my length of x should be 100 How do i do that?? Sorry for the inconvenience.  
Thanks for any advice or help.

---

<div class="post-metadata">

**Author:** ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)\
**Post date:** [January 13, 2023, 4:04am UTC](https://discourse.julialang.org/t/creating-a-list-of-100-binomial-distributed-values/92903/2 "2023-01-13T04:04:28Z")

</div>

Edit: deleting as the post question was rewritten and changed.

---

<div class="post-metadata">

**Author:** ![chiion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chiion/32/37002_2.png) [@chiion](https://discourse.julialang.org/u/chiion)\
**Post date:** [January 13, 2023, 5:44am UTC](https://discourse.julialang.org/t/creating-a-list-of-100-binomial-distributed-values/92903/3 "2023-01-13T05:44:51Z")

</div>

An easy way to do this is by scaling `rand()`:

```julia
x = 2*rand(0:1, 100) .-1

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [January 13, 2023, 5:56am UTC](https://discourse.julialang.org/t/creating-a-list-of-100-binomial-distributed-values/92903/4 "2023-01-13T05:56:35Z")

</div>

```julia
x = rand((-1, 1), 100)

```

?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [January 13, 2023, 6:11am UTC](https://discourse.julialang.org/t/creating-a-list-of-100-binomial-distributed-values/92903/5 "2023-01-13T06:11:53Z")

</div>

> [@BadBoy](#):
>
> `rand(Binomial(1,0.5),1)[1]`

BTW, this is needlessly complicated. You are creating a length-1 vector, and reading out the first element. It’s slow, too.

Instead, just directly generate a scalar value:

```julia
rand(Binomial(1,0.5))

```

Similarly, you can use `rand()`, not `rand(1)[1]`.

---

<div class="post-metadata">

**Author:** ![BadBoy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/badboy/32/202815_2.png) [@BadBoy](https://discourse.julialang.org/u/BadBoy)\
**Post date:** [January 13, 2023, 10:16am UTC](https://discourse.julialang.org/t/creating-a-list-of-100-binomial-distributed-values/92903/6 "2023-01-13T10:16:58Z")

</div>

Thanks for your help. It is working now.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [January 13, 2023, 12:09pm UTC](https://discourse.julialang.org/t/creating-a-list-of-100-binomial-distributed-values/92903/7 "2023-01-13T12:09:51Z")

</div>

> [@chiion](#):
>
> `rand(0:1, 100)`

Generating rands from a range isn’t optimal. Here are some comparisons:

```julia
julia> @btime rand(0:1, 100);
  656.522 ns (1 allocation: 896 bytes)

julia> @btime rand((0, 1), 100);
  161.224 ns (1 allocation: 896 bytes)

julia> @btime rand(Bool, 100);
  48.485 ns (1 allocation: 160 bytes)

```

So the fastes solution here uses something almost like your suggestion, @chiion. It has slightly more allocations than `rand((-1, 1), 100)`:

```julia
(rand(Bool, 100) .* 2) .- 1

```

---

<div class="post-metadata">

**Author:** ![chiion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chiion/32/37002_2.png) [@chiion](https://discourse.julialang.org/u/chiion)\
**Post date:** [January 13, 2023, 1:19pm UTC](https://discourse.julialang.org/t/creating-a-list-of-100-binomial-distributed-values/92903/8 "2023-01-13T13:19:22Z")

</div>

👍

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [January 13, 2023, 2:19pm UTC](https://discourse.julialang.org/t/creating-a-list-of-100-binomial-distributed-values/92903/9 "2023-01-13T14:19:02Z")

</div>

> [@DNF](#):
>
> `(rand(Bool, 100) .* 2) .- 1`

And don’t forget the similar:

```julia
(-1).^rand(Bool, 100)

```

which yields the same result but perhaps looks neater.

---

<div class="post-metadata">

**Author:** ![BadBoy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/badboy/32/202815_2.png) [@BadBoy](https://discourse.julialang.org/u/BadBoy)\
**Post date:** [January 13, 2023, 2:22pm UTC](https://discourse.julialang.org/t/creating-a-list-of-100-binomial-distributed-values/92903/10 "2023-01-13T14:22:21Z")

</div>

ok. Thanks
