# While loop to evaluate until conditions are false

**URL:** <https://discourse.julialang.org/t/while-loop-to-evaluate-until-conditions-are-false/69446>\
**Category:** General Usage\
**Created:** [October 8, 2021, 5:49pm UTC](https://discourse.julialang.org/t/while-loop-to-evaluate-until-conditions-are-false/69446 "2021-10-08T17:49:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [October 8, 2021, 5:49pm UTC](https://discourse.julialang.org/t/while-loop-to-evaluate-until-conditions-are-false/69446/1 "2021-10-08T17:49:53Z")

</div>

Hi, I’ve been stumped for a while now and think I am overthinking this.

I want to have a vector that excludes a certain value (in this case 2) AND each element is unique (no repeats). My thought is that while my two conditions are true, the while loop should keep running until they are both false. This is probably the wrong logic, so I need to ask for advice.

```julia
QWER = rand(1:6, 4)

while QWER === 2 || unique(QWER) !== 4
    QWER = rand(1:6, 4)
end

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 8, 2021, 5:57pm UTC](https://discourse.julialang.org/t/while-loop-to-evaluate-until-conditions-are-false/69446/2 "2021-10-08T17:57:21Z")

</div>

just use:  
[Sampling from Population · StatsBase.jl](https://juliastats.org/StatsBase.jl/stable/sampling/#StatsBase.sample) with `;replace=flase`

to make your example work, probably do this:

```julia
using Random

QWER = rand(1:6, 4)

while in(2, QWER) || length(unique(QWER))!=4
    rand!(QWER,1:6)
end

```

---

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [October 8, 2021, 6:26pm UTC](https://discourse.julialang.org/t/while-loop-to-evaluate-until-conditions-are-false/69446/3 "2021-10-08T18:26:21Z")

</div>

You can try something like

```julia
mvec = filter(x -> x !=2, 1:6)
sample(mvec, 4, replace=false)

```

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [October 8, 2021, 6:42pm UTC](https://discourse.julialang.org/t/while-loop-to-evaluate-until-conditions-are-false/69446/4 "2021-10-08T18:42:38Z")

</div>

`QWER` will never `=== 2` because it’s not even an `Integer`.

`rand([1,3,4,5,6], 4)` will solve your first requirement. `StatsBase.sample([1,3,4,5,6], 4; replace=false)` does both.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [October 8, 2021, 6:57pm UTC](https://discourse.julialang.org/t/while-loop-to-evaluate-until-conditions-are-false/69446/5 "2021-10-08T18:57:13Z")

</div>

One could shuffle it:

```julia
a =[1, 3, 4, 5, 6]
shuffle(a)[1:4]

```

or:

```julia
a = 1:6
b = a .!= 2
shuffle(a[b])[1:4]

```

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [October 8, 2021, 7:56pm UTC](https://discourse.julialang.org/t/while-loop-to-evaluate-until-conditions-are-false/69446/6 "2021-10-08T19:56:53Z")

</div>

Thank you for your replies.
