You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importnumpyasnpimporttimeBATCH_SIZE=2048# Draw random pairs (x, y) in batches of BATCH_SIZE elementsN_BATCHES=1024# Total number of batchesN=N_BATCHES*BATCH_SIZE# Total number of random pairs (x, y)defmonte_carlo_pi_batch():
x=np.random.random(BATCH_SIZE)
y=np.random.random(BATCH_SIZE)
acc=np.count_nonzero(x*x+y*y<=1.0)
return4.0*acc/BATCH_SIZEdefmonte_carlo_pi():
s=0.0foriinrange(N_BATCHES):
s+=monte_carlo_pi_batch()
returns/N_BATCHESdefmain():
print("Using device ...")
t1=time.time()
pi=monte_carlo_pi()
t2=time.time()
print("Pi =", pi)
print("Done in ", t2-t1, "seconds...")
if__name__=="__main__":
main()