# Random numbers, why is it necessary to multiply by rand()

**URL:** <https://discourse.julialang.org/t/random-numbers-why-is-it-necessary-to-multiply-by-rand/24539>\
**Category:** New to Julia\
**Created:** [May 23, 2019, 8:50pm UTC](https://discourse.julialang.org/t/random-numbers-why-is-it-necessary-to-multiply-by-rand/24539 "2019-05-23T20:50:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![isamuelsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/isamuelsson/32/8854_2.png) [@isamuelsson](https://discourse.julialang.org/u/isamuelsson)\
**Post date:** [May 23, 2019, 8:50pm UTC](https://discourse.julialang.org/t/random-numbers-why-is-it-necessary-to-multiply-by-rand/24539/1 "2019-05-23T20:50:20Z")

</div>

Hi I am working on the pi example in JuliaBox. When I am generating random numbers the only way I do not get 0 as the value of pi is if I multiply by rand() (as it did in the solution to the exercise). It looks like multiplying rand() makes x and y decimals. I was wondering how multiplying rand() was used or what it specifically does?

function calculate\_pi(N,r)  
Acircle = 0

```
for i in 1:N    
    x, y = rand([-1, 1])*rand(), rand([-1, 1])*rand()

    dist = sqrt(x^2 + y^2)

    dist < r ? Acircle += 1 : Acircle += 0 
end
return 4*(Acircle/N)

```

end

N = 10000  
r = 1  
calculate\_pi(N, r)

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [May 23, 2019, 8:56pm UTC](https://discourse.julialang.org/t/random-numbers-why-is-it-necessary-to-multiply-by-rand/24539/2 "2019-05-23T20:56:47Z")

</div>

for what i see, rand(Vector) gives an random element of the vector, and rand([-1,1]) is either -1 or 1, so that operation is to give the x and y coordinates a random sign

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [May 23, 2019, 9:11pm UTC](https://discourse.julialang.org/t/random-numbers-why-is-it-necessary-to-multiply-by-rand/24539/3 "2019-05-23T21:11:44Z")

</div>

You could also express something comparable as `2*rand()-1` — it’s just extending the range of generated values such that it covers the entire unit circle instead of just the upper right quadrant.

That said, the formula works just as well if you use `x, y = rand(), rand()`! That just looks at the upper right quadrant, but it works because you’re symmetrically reducing the areas of the circle and the square by the same fraction. 🙂

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [May 23, 2019, 10:06pm UTC](https://discourse.julialang.org/t/random-numbers-why-is-it-necessary-to-multiply-by-rand/24539/4 "2019-05-23T22:06:45Z")

</div>

If you care about performance, you should absolutely drop the `rand([-1, 1])`. It makes no difference to the result, and it makes your code almost 10x slower. Most of that is due to the vectors `[-1, 1]` you keep creating over and over, if you keep the `rand`s but replace them with `rand((-1, 1))` instead, you get a very large speed-up, since tuples are so efficient.

But, even so, just drop them entirely.

You can also get some improvement if you realize that

```julia
sqrt(x^2 + y^2) < r

```

is equivalent to

```julia
x^2 + y^2 < r^2 # and here you could just pre-calculate r^2

```

No need to calculate the `sqrt`.

Furthermore, you can add a `Bool` to an `Int`. Doesn’t seem to matter much to performance, but it’s more elegant, imo:

```julia
Acircle += (x^2 + y^2 < r^2)

```

---

<div class="post-metadata">

**Author:** ![spaceLem](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/spacelem/32/217628_2.png) [@spaceLem](https://discourse.julialang.org/u/spaceLem)\
**Post date:** [May 24, 2019, 1:49pm UTC](https://discourse.julialang.org/t/random-numbers-why-is-it-necessary-to-multiply-by-rand/24539/5 "2019-05-24T13:49:55Z")

</div>

If you care even more about performance, there’s no need to actually store `x` and `y`.

```julia
function calc_pi(N, r)
    Acirc = 0
    for i in 1:N
        Acirc += rand()^2 + rand()^2 < r^2
    end
    4Acirc / N
end

```

Running `@btime` on those:

```julia
julia> @btime calculate_pi(10^4, 1)
  972.581 μs (20000 allocations: 1.83 MiB)
3.136

julia> @btime calc_pi(10^4, 1)
  53.120 μs (0 allocations: 0 bytes)
3.144

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 24, 2019, 2:31pm UTC](https://discourse.julialang.org/t/random-numbers-why-is-it-necessary-to-multiply-by-rand/24539/6 "2019-05-24T14:31:45Z")

</div>

> [@spaceLem](#):
>
> If you care even more about performance, there’s no need to actually store `x` and `y` .

Assigning something to a local variable is just giving it a name. It has no effect on how/where the value is stored or on performance.

---

<div class="post-metadata">

**Author:** ![spaceLem](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/spacelem/32/217628_2.png) [@spaceLem](https://discourse.julialang.org/u/spaceLem)\
**Post date:** [May 24, 2019, 3:01pm UTC](https://discourse.julialang.org/t/random-numbers-why-is-it-necessary-to-multiply-by-rand/24539/7 "2019-05-24T15:01:31Z")

</div>

@stevengj Eh… I checked again and you’re right. Good to know!

I guess it saves a few keystrokes though 🙂
