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)
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
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.
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 rands 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
sqrt(x^2 + y^2) < r
is equivalent to
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: