# While loop until value falls within a range

**URL:** <https://discourse.julialang.org/t/while-loop-until-value-falls-within-a-range/44330>\
**Category:** New to Julia\
**Created:** [August 5, 2020, 12:34pm UTC](https://discourse.julialang.org/t/while-loop-until-value-falls-within-a-range/44330 "2020-08-05T12:34:22Z")\
**Posts on this page:** 7\
**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:** [August 5, 2020, 12:34pm UTC](https://discourse.julialang.org/t/while-loop-until-value-falls-within-a-range/44330/1 "2020-08-05T12:34:22Z")

</div>

I’d like to be able to randomly pick a species that has a trophic level within a certain range.

```julia
num_animals = 40
trophic_position = rand(num_animals)

function target_species_check_change()
    sp = rand((num_animal),1)
    while (trophic_position[sp] < 0.3) && (trophic_position[sp] > 0.4)
        sp = rand((num_animal),1)
    end
    return sp
end

target_species = target_species_check_change()

```

I would just want the number of the species (1:40) to be returned which corresponds to a trophic level between 0.3 and 0.4

---

<div class="post-metadata">

**Author:** ![mikkoku](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikkoku/32/16274_2.png) [@mikkoku](https://discourse.julialang.org/u/mikkoku)\
**Post date:** [August 5, 2020, 1:33pm UTC](https://discourse.julialang.org/t/while-loop-until-value-falls-within-a-range/44330/2 "2020-08-05T13:33:00Z")

</div>

The syntax to pick a random integer within a range is

```julia
sp = rand(1:num_animals)

```

If you have other questions please ask.

---

<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:** [August 5, 2020, 1:54pm UTC](https://discourse.julialang.org/t/while-loop-until-value-falls-within-a-range/44330/3 "2020-08-05T13:54:20Z")

</div>

Thank you for your reply, I have updated by code below. I am unable to generate a `target_species` that has a `trophic_position` between `0.3` and `0.4`.

```julia
num_animals2 = 40
trophic_position2 = rand(num_animals2)

function target_species_check_change2()
    sp = rand((1:num_animals2),1)[1]
    while (trophic_position2[sp] < 0.3) && (trophic_position2[sp] > 0.4)
        sp = rand((1:num_animals2),1)[1]
    end
    return sp
end

target_species = target_species_check_change()
trophic_position[target_species]

```

---

<div class="post-metadata">

**Author:** ![sostock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sostock/32/5546_2.png) [@sostock](https://discourse.julialang.org/u/sostock)\
**Post date:** [August 5, 2020, 2:13pm UTC](https://discourse.julialang.org/t/while-loop-until-value-falls-within-a-range/44330/4 "2020-08-05T14:13:06Z")

</div>

You have to use `||` instead of `&&`, or the `while` loop will never execute (since there is no number that is smaller than 0.3 and larger than 0.4).

Note that you can also replace `rand((1:num_animals2),1)[1]` by `rand(1:num_animals2)`. Right now you are allocating an unnecessary 1-element array every time.

---

<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:** [August 5, 2020, 2:22pm UTC](https://discourse.julialang.org/t/while-loop-until-value-falls-within-a-range/44330/5 "2020-08-05T14:22:44Z")

</div>

Thank you for your reply. I though `||` means or from [here](https://docs.julialang.org/en/v1/manual/control-flow/#Short-Circuit-Evaluation-1).

I understand why `&&` would not work from your explanation. There is probably a less confusing way to write my function.

---

<div class="post-metadata">

**Author:** ![sostock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sostock/32/5546_2.png) [@sostock](https://discourse.julialang.org/u/sostock)\
**Post date:** [August 5, 2020, 2:36pm UTC](https://discourse.julialang.org/t/while-loop-until-value-falls-within-a-range/44330/6 "2020-08-05T14:36:21Z")

</div>

> [@HelgavonLichtenstein](#):
>
> I though `||` means or from [here](https://docs.julialang.org/en/v1/manual/control-flow/#Short-Circuit-Evaluation-1).

It _does_ mean “or”, which is what you want here.

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [August 5, 2020, 3:15pm UTC](https://discourse.julialang.org/t/while-loop-until-value-falls-within-a-range/44330/7 "2020-08-05T15:15:00Z")

</div>

I believe the following code does what you want more cleanly. I replaced your `while` loop with elementwise broadcasting indicated by `.`.

```julia
num_animals=40
trophic_position=rand(num_animals) #Create trophic position data for each animal
trophic_position_within_range=(trophic_position.>0.3) .& (trophic_position.<0.4) #Vector of true/false
animals_within_range=(1:num_animals)[trophic_position_within_range] #Vector of animals where trophic_position_within_range is true
random_animal_in_range=rand(animals_within_range) #Selects a random animal from the previous vector

```

or as a function:

```julia
function select_animal(num_animals,lower_bound,upper_bound)
    trophic_position=rand(num_animals)
    trophic_position_within_range=(trophic_position.>lower_bound) .& (trophic_position.<upper_bound)
    animals_within_range=(1:num_animals)[trophic_position_within_range]
    random_animal_in_range=rand(animals_within_range)
end

chosen_animal=select_animal(40,0.3,0.4)

```
