# Using random\_agent with a condition

**URL:** <https://discourse.julialang.org/t/using-random-agent-with-a-condition/104229>\
**Category:** Modelling & Simulations\
**Tags:** agentsjl\
**Created:** [September 25, 2023, 2:29pm UTC](https://discourse.julialang.org/t/using-random-agent-with-a-condition/104229 "2023-09-25T14:29:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![daler6](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daler6/32/13930_2.png) [@daler6](https://discourse.julialang.org/u/daler6)\
**Post date:** [September 25, 2023, 2:29pm UTC](https://discourse.julialang.org/t/using-random-agent-with-a-condition/104229/1 "2023-09-25T14:29:54Z")

</div>

I am working on reproducing the Zero Information trader in Julia using Agents.jl. My agents include a field named :traded, which is of type Bool and is initialized to false for all agents. This is changed to true once an agent has been involved in a trade. In the model\_step! function, I need to select a random agent that has not been involved in a trade. The code I have used is as follows:

```julia
trading_agent = random_agent(model, :traded == false)

```

where model is the instantiated model passed to the model\_step! function.

This results in a MethodError indicating that object of type Bool are not callable. Looking at the documentation for Agents.jl I could not find an example where random\_agent was used with a condition; nor could I find one in the Example Zoo, So, I tried a number of variations, to no avail, and finally used the following workaround, which is:

```julia
i = true
while i == true
     trading_agent = random_agent(model)
     i = trading_agent.traded
end

```

This works but is not very elegant and still leaves me wondering how to properly use random\_agent.

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [September 25, 2023, 4:02pm UTC](https://discourse.julialang.org/t/using-random-agent-with-a-condition/104229/2 "2023-09-25T16:02:38Z")

</div>

`:traded == false` is not a function. It is an expression which is equivalent with the constant literal `false`. As `:traded` is a symbol, and `false` is a boolean, they can never be equal to each other.

You need to provide **a function** as a second argument to `random_agent.`

Try `random_agent(model, agent -> agent.traded == false)` instead. The expression `agent -> agent.traded == false` creates an anonymous function taht given an agent it returns `true` if it hasn’t traded yet.

---

<div class="post-metadata">

**Author:** ![daler6](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daler6/32/13930_2.png) [@daler6](https://discourse.julialang.org/u/daler6)\
**Post date:** [September 25, 2023, 6:31pm UTC](https://discourse.julialang.org/t/using-random-agent-with-a-condition/104229/3 "2023-09-25T18:31:59Z")

</div>

Works perfectly. Thanks.
