# Rand does not return expected confidence interval

**URL:** https://discourse.julialang.org/t/rand-does-not-return-expected-confidence-interval/123053
**Category:** Optimization (Mathematical)
**Created:** [November 25, 2024, 6:42pm UTC](https://discourse.julialang.org/t/rand-does-not-return-expected-confidence-interval/123053 "2024-11-25T18:42:10Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Trylmand](https://avatars.discourse-cdn.com/v4/letter/t/ea666f/32.png) [@Trylmand](https://discourse.julialang.org/u/Trylmand)
#### Post date: [November 25, 2024, 6:42pm UTC](https://discourse.julialang.org/t/rand-does-not-return-expected-confidence-interval/123053/1 "2024-11-25T18:42:10Z")

</div>

Dear all,  
Testing Rand() on MC algorithm for estimating integral of Sine from 0 to Pi,  
only returns 50% of the expected confidence interval.  
Can anybody see, where my code breaks?

n = 1000  
in\_ci = 0  
for i in 1:500  
sin\_arr = map(x → sin(x), pi_rand(n))  
lower = pi_mean(sin\_arr) - 1.96_std(sin\_arr)/sqrt(n)  
upper = pi_mean(sin\_arr) + 1.96\*std(sin\_arr)/sqrt(n)  
if (lower \<= 2) && (upper \>= 2)  
in\_ci += 1  
end  
end  
println("For n = ", n, " share of estimates in CI.: ", in\_ci/500)

Result.:  
For n = 1000 share of estimates in CI.: 0.468

---

<div class="post-metadata">

### Author: ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)
#### Post date: [November 25, 2024, 6:53pm UTC](https://discourse.julialang.org/t/rand-does-not-return-expected-confidence-interval/123053/2 "2024-11-25T18:53:06Z")

</div>

Welcome to the forum! Note that you can wrap your code in triple backticks (`) to properly quote your code.

I think you will get the correct result if you also multiply your standard error by `pi`?

---

<div class="post-metadata">

### Author: ![Trylmand](https://avatars.discourse-cdn.com/v4/letter/t/ea666f/32.png) [@Trylmand](https://discourse.julialang.org/u/Trylmand)
#### Post date: [November 25, 2024, 7:02pm UTC](https://discourse.julialang.org/t/rand-does-not-return-expected-confidence-interval/123053/3 "2024-11-25T19:02:32Z")

</div>

Thx, got blind staring at the code 🙂
