We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
# X ~ N(30, 4**2)mu,sig=30,4INF=float('inf')cases=[(-INF,40),(21,INF),(30,35)]defp(f:float):print(f'{f:.3f}')``### Method 1: bruteforce approximportrandomdefsample():returnrandom.gauss(mu,sig)# you can also sample randomly by taking 2 outputs of random.random() && using the Box-Muller formula.# if `random` itself is banned, you can try making a simple LCG from scratch, but it may be too biased.defbrute(a,b,tries:int=99999):returnsum(a<sample()<bfor_inrange(tries))/triesforargsincases:p(brute(*args))``### Method 2: \Phi(z) = (1+erf(z/\sqrt{2}))/2importmathdefcdf_N(z:float):return(1+math.erf(z/math.sqrt(2)))/2defcdf_X(z:float):returncdf_N((z-mu)/sig)p(cdf_X(40))p(1-cdf_X(21))p(cdf_X(35)-cdf_X(30))``### Method 3: Riemann summation# pdf(t) = \frac{e^{-t^2/2}}{\sqrt{2\pi}}defpdf(t:float):returnmath.exp(-t*t/2)/math.sqrt(2*math.pi)defphi(t):#approximatetheCDFt_abs=abs(t)delta,eps=1e-3,1e-6s=0while(d:=delta*pdf(t_abs))>=eps:s+=dt_abs+=deltareturnsift<=0else1-s#\Phi(t)=1-\Phi(t)ift>0defcdf(z):returnphi((z-mu)/sig)p(cdf(40))p(1-cdf(21))p(cdf(35)-cdf(30)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 4: Normal Distribution #1
You are viewing a single comment's thread. Return to all comments →
Based on the solutions of the past: