While loop to evaluate until conditions are false

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.

QWER = rand(1:6, 4)

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

just use:
Sampling from Population · StatsBase.jl with ;replace=flase

to make your example work, probably do this:

using Random

QWER = rand(1:6, 4)

while in(2, QWER) || length(unique(QWER))!=4
1 Like

You can try something like

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

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.

1 Like

One could shuffle it:

a =[1, 3, 4, 5, 6]


a = 1:6
b = a .!= 2

Thank you for your replies.