# Multithreading and rand within a specific range

**URL:** <https://discourse.julialang.org/t/multithreading-and-rand-within-a-specific-range/31567>\
**Category:** New to Julia\
**Created:** [November 27, 2019, 9:40am UTC](https://discourse.julialang.org/t/multithreading-and-rand-within-a-specific-range/31567 "2019-11-27T09:40:36Z")\
**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:** [November 27, 2019, 9:40am UTC](https://discourse.julialang.org/t/multithreading-and-rand-within-a-specific-range/31567/1 "2019-11-27T09:40:36Z")

</div>

Hello,

I am trying to find parameters that make my model work using brute force. To do this I need to run my model many times varying the parameters each time. It works when I do it without multithreading but I want to run many more trials.

I’ve been reading the [Parallel Computing doc](https://docs.julialang.org/en/v1/manual/parallel-computing/index.html#Side-effects-and-mutable-function-arguments-1) as well as these forums so I know I need to have an individual rand for each thread but I am not sure how to do this. Julia is my first computing language and (I think) I have a basic understanding of programming.

```julia
 using Random; import Future 

function parameter_search2()
    for i in 1:1000000
    # Threads.@threads for i in 1:1000000 # this one
    # @threads for i in 1:1000000 # or this one?
        
        bioS = sol[end] # this is generated once, outside of this function
        target_species = 42
       
        catch_max2 = rand(Uniform(0,1)) # should it be rand([threadid()Uniform(0,1)]) ?
        scaling2 = rand(Uniform(0,1))
        maintenance2 = rand(Uniform(0,0.5))
        P_catch2 = rand(Uniform(0,0.5))
        μ2 = rand(Uniform(0,0.5))

        # after this point I run a differential equation using these random value inputs;
        # and then println when a criteria is reached
end

```

Any advice or example is appreciated.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [November 27, 2019, 9:45am UTC](https://discourse.julialang.org/t/multithreading-and-rand-within-a-specific-range/31567/2 "2019-11-27T09:45:40Z")

</div>

`Threads.@threads` should work. You would also have to store the parameters that are successful in a threadsafe way, see e.g., a `ReentrantLock`

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [November 27, 2019, 9:45am UTC](https://discourse.julialang.org/t/multithreading-and-rand-within-a-specific-range/31567/3 "2019-11-27T09:45:58Z")

</div>

> [@HelgavonLichtenstein](#):
>
> I know I need to have an individual rand for each thread

This is actually no longer true. The global RNG is now thread safe, the docs are just not updated ([remove incorrect examples about thread-unsafe functions in docs by KristofferC · Pull Request #33419 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/33419)).

---

<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:** [November 27, 2019, 10:01am UTC](https://discourse.julialang.org/t/multithreading-and-rand-within-a-specific-range/31567/4 "2019-11-27T10:01:00Z")

</div>

Thank you for your quick replies!

@baggepinnen Are you referring to something like [this](https://docs.julialang.org/en/v1/manual/faq/#faq-async-io-1) ?

@kristoffer.carlsson Does this mean I can include `Threads.@threads for i in 1:1000000` and leave it written as `catch_max2 = rand(Uniform(0,1))` ?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [November 27, 2019, 10:08am UTC](https://discourse.julialang.org/t/multithreading-and-rand-within-a-specific-range/31567/5 "2019-11-27T10:08:45Z")

</div>

It depends a bit on how Distributions has implemented `rand` on their types but as long as they just use the global random number generator from Julia, it should be fine,.

---

<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:** [November 27, 2019, 10:17am UTC](https://discourse.julialang.org/t/multithreading-and-rand-within-a-specific-range/31567/6 "2019-11-27T10:17:28Z")

</div>

Thanks!
